Import resolution doesn't understand editable installation with a non-standard `package-dir`.
Environment data
- Language Server version: 2023.12.1
- OS and version: win32 x64
- Python version (and distribution if applicable, e.g. Anaconda):
- python.analysis.indexing: true
- python.analysis.typeCheckingMode: off
Code Snippet
from myproj.module import variable
If myproj is imported in editable mode and there isn't a myproj/ directory anywhere, pylance marks it as a missing import.
Repro Steps
Create a directory structure like
Project/
.venv/
src/
__init__.py
module.py
pyproject.toml
Where pyproject.toml is
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "myproj"
version = "1"
[tool.setuptools.package-dir]
myproj = "src"
__init__.py is
from myproj.module import variable
from src.module import variable2
and the venv is created with python -m venv .venv. Run .\.venv\Scripts\activate and then pip install -e ..
Expected behavior
Pylance should inform me that src.module is an error via reportMissingImports, while myproj.module should be understood as valid.
Actual behavior
It does the opposite; it likes src.module, while throwing reportMissingImports at myproj.module.
Logs
There's not really any relevant logs.
I'm aware that either
- You're going to tell me about some setting I couldn't find which uses your venv's path to resolve imports rather than finding modules.
- This esssentially constitutes a massive feature request.
It's just been bothering me for weeks. Thank you for your help!
What's in the .pth file for your editable install? It needs to be just a path for us to support it. We don't support the use of import hooks. See https://microsoft.github.io/pyright/#/import-resolution?id=editable-installs
Is your venv the selected interpreter within VS Code? See the "Python: Select Interpreter" command in the command palette.
The .pth file is
import __editable___myproj_1_finder; __editable___myproj_1_finder.install()
which corresponds to a file __editable___myproj_1_finder.py which seems to be created by Python by default. At the top if it it says
MAPPING = {'myproj': 'C:\\Users\\Andrew\\Code\\Issue\\src'}
NAMESPACES = {}
PATH_PLACEHOLDER = '__editable__.myproj-1.finder' + ".__path_hook__
And the rest of the file seems to be boilerplate / not specific to myproj.
My default Python interpreter path is "python.defaultInterpreterPath": "${workspaceFolder}/.venv" and choosing .venv/Scripts.python.exe doesn't change anything for me.
For reference, I'm using Python 3.12.1
I installed it in compat mode, so now the __editable__.myproj-1.pth file is
C:\Users\Andrew\Code\Project
However, nothing has changed, as I'm pretty sure the Project directory is already automatically in the path. It still doesn't handle the translation between src and myproj.
I see. We don't support that. Our support for editable installs is limited to simple (non-hook) .pth files -- the paths in them are automatically added (by the interpreter) to the path we use for import resolution.
You could make your scenario work by adding a symbolic link: mklink /D myproj src
Improving editable install support isn't a high priority for us at the moment. If you'd like, I can turn this into an enhancement discussion and we can see if it gets enough up-votes to get onto our radar.
Moving this issue to discussion as an enhancement request for comments and upvotes.