poetry dependencies fail to import relative packages
See original GitHub issue-
I am on the latest Poetry version.
-
I have searched the issues of this repo and believe that this is not a duplicate.
-
If an exception occurs when executing a command, I executed it again in debug mode (
-vvv
option). -
OS version and name: linux
-
Poetry version: 0.12.17
Issue
There’s a very peculiar issue that I just can’t replicate. It happens when poetry is using a dependency that does relative imports.
Lets assume we have package X that comes with an entry point and does this:
# main.py
from importlib import import_module
import_module('tests.sometest')
Our poetry project in pyproject.toml
has:
[tool.poetry.dependencies]
packagex = "^1.0"
we also have this project tree:
├── pyproject.toml
└── tests
└── sometest.py
Finally if we run poetry run packagex
we’ll see:
ModuleNotFoundError: No module named 'tests.sometest'
However if we run poetry run python
and do the same routine manually it will import just fine:
from importlib import import_module
import_module('tests.sometest')
<module 'tests.sometest' from '/home/...'>
There is a lot of inconsistency with this behaviour. Sometimes clearing all poetry data and doing a fresh poetry install
helps, most of the times explicitly specifying PYTHONPATH
helps:
export PYTHONPATH=`pwd`
I can’t reliably replicate this issue - there seems to be some weirdness going on with PYTHONPATH as if dependency entry points don’t consider current working directory as part of PYTHONPATH
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:5 (2 by maintainers)
Top GitHub Comments
I have a similar issue: My module directory looks like
In
testmodule.py
I importconfig.py
like this:The script defined in my
pyproject.toml
looks like this:When run
poetry run testmodule
I get the following Error:Weirdly enough this works however:
(this uses a
main()
function call in aif __name__ == "__main__":
block)Closing this because it’s not a poetry issue. It is about how
import
works in the python land.