Relative auto-import top level #4802
Replies: 3 comments 1 reply
-
Moving this issue to discussion as an enhancement request for comments and upvotes. |
Beta Was this translation helpful? Give feedback.
-
I've been getting this for ages both in my work and open source codebases, both using Poetry and PDM. If a project contains multiple packages (or just a main package and another one for tests), using relative imports in Pylance simply doesn't work properly. The generated relative import is too deep and doesn't work at runtime. This makes the auto import feature with relative imports much less useful since I need to go back to the top of the file and fix them constantly. Here's an example of me trying to write a unit test for my cattrs library: It doesn't seem to me like there's much to discuss here. |
Beta Was this translation helpful? Give feedback.
-
Linking to this which is the same issue: #6467 |
Beta Was this translation helpful? Give feedback.
-
As an enthusiastic user of the auto-import completion, there is one thing I couldn't manage to deal with: when setting the import format to relative (
"python.analysis.importFormat": "relative"
). there are still cases that need local absolute imports.For instance in a project with multiple python packages or a separate tests folder:
Importing from
mypackage
intest_foo.py
results in a relative importfrom ..mypackage import myfunc
which is not what is wanted.In short the idea is to have a way to switch to absolute imports in certain cases, either with some manual configuration or by detecting when the import goes beyond the root of the package.
Beta Was this translation helpful? Give feedback.
All reactions