1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Poetry+pyenv環境で自作のライブラリをimportしようとして詰まった

Posted at

自作のパッケージを認識してくれない

こういう構造のディレクトリでの話

work_dir
├── pyproject.toml
├── src
│   └── my_package
│       ├── __init__.py
│       └── my_module.py
└── work.py

work_dir内のwork.pysrc/my_package以下にある自作パッケージを以下のように利用しようとしたら怒られた

python work.py
import sys 
sys.path.append("path/to/src")

from my_package import my_module

解決策

pyproject.tomlに以下の内容を書き込んであげる

[tool.poetry]
packages = [
    { include = "my_module", from = "src" },
]

poetry installを忘れずに

$ poetry install

これでPoetry環境で自作のパッケージを利用できた。

O'reillyの著書に付随したGithubのコードを流用した時にこれが起きた。sys.path.appendでライブラリを認識させる方法はそもそも修正コストがめちゃ大きいのであまり良くないらしい。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?