Skip to content
Advertisement

import local package during poetry run

I just transitioned from pipenv to poetry and I’m having trouble importing a package from a local package I’m developing in a few of my scripts. To make this more concrete, my project looks something like:

pyproject.toml
poetry.lock
bin/
  myscript.py
mypackage/
  __init__.py
  lots_of_stuff.py

Within myscript.py, I import mypackage. But when I poetry run bin/myscript.py I get a ModuleNotFoundError because the PYTHONPATH does not include the root of this project. With pipenv, I could solve that by specifying PYTHONPATH=/path/to/project/root in a .env file, which would be automatically loaded at runtime. What is the right way to import local packages with poetry?

I ran across this piece on using environment variables but export POETRY_PYTHONPATH=/path/to/roject/root doesn’t seem to help.

Advertisement

Answer

After quite a bit more googling, I stumbled on the packages attribute within the tool.poetry section for pyproject.toml files. To include local packages in distribution, you can specify

# pyproject.toml

[tool.poetry]
# ...
packages = [
    { include = "mypackage" },
]

Now these packages are installed in editable mode :)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement