question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
atoavcommented, Apr 28, 2020

I have a similar issue: My module directory looks like

testmodule
├── __init__.py
├── testmodule.py
└── config.py

In testmodule.py I import config.py like this:

from config import Config

The script defined in my pyproject.toml looks like this:

[tool.poetry.scripts]
testmodule = "testmodule.testmodule:main"

When run poetry run testmodule I get the following Error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/mnt/e/Coding/work/testmodule/testmodule/testmodule.py", line 12, in <module>
    from config import Config
ModuleNotFoundError: No module named 'config'

Weirdly enough this works however:

poetry run python3 testmodule/testmodule.py

(this uses a main() function call in a if __name__ == "__main__": block)

0reactions
finswimmercommented, Jul 23, 2021

Closing this because it’s not a poetry issue. It is about how import works in the python land.

Read more comments on GitHub >

github_iconTop Results From Across the Web

poetry dependencies fail to import relative packages #1580
It happens when poetry is using a dependency that does relative imports. Lets assume we have package X that comes with an entry...
Read more >
Imports from a poetry path dependency does not resolve
I have a repo with two python projects in it, both poetry projects. One project imports the other using a relative path dependency...
Read more >
Publishing a poetry-managed python package with relative ...
I'm trying to come up with a solution to what seems like a pretty mundane task: develop a python package with relative dependencies, ......
Read more >
History | Poetry - Python dependency management and ...
Fix an issue where relative paths were encoded into package requirements, instead of a file:// URL as required by PEP 508 (#512). poetry-plugin-export...
Read more >
Which is the right way to use modules within a package?
There is a nice article about this in Absolute vs Relative Imports in Python – Real Python. ... And after poetry install to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found