pythonpipvirtualenvpylancepyright

Pylance does not recognize local packages installed with "pip install -e "


I have the following project setup (names changed):

ProjectRoot   
│           
├───DependencyPackage
│   │   .gitignore
│   │   Dockerfile
│   │   LICENSE
│   │   pyproject.toml
│   │   README.md
│   │   setup.cfg
│   │   setup.py
│   │               
│   └───dependency_source
│       │   __init__.py
│       │   
│       ├───models
│       │       first_model.py
│       │       second_model.py
│       │       
│       └───code
│               some_logic.py
│                                     
├───MainPackage
│   │   .gitignore
│   │   Dockerfile
│   │   LICENSE
│   │   pyproject.toml
│   │   README.md
│   │   setup.cfg
│   │   setup.py
│   │         
│   │       
│   └───main_source
│           main.py
│           __init__.py
│           
└───venv

In Visual Studio Code the interpreter is chosen via path to venv/Scripts/python.exe, both MainPackage as well as DependencyPackage are installed in the virtual environment via pip install -e ".[dev]", they also show up with the correct path and version in pip list

The dependency package is listed as install_requires = dependency_package and I have checked all the naming (e.g. - vs _) several times.

Additionally the code runs fine if I for example use in the MainPackage/main_source/main.py:

from dependency_package.code.some_logic import myfunction
    
def main():
    myfunction("something something...")
   
if __name__ == "__main__":
    main()

However, even though everything clearly works, I still get the following pylance error on import:

Import "dependency_package.code.some_logic" could not be resolved furthermore, Intellisense fails and I get no code completion and so on...

Clearly I am missing something here.

addition: Just tested again, installing with pip install (no -e), will work fine and my imports are recognized. I do however need the -e flag, since I am developing on both packages simultaneously.


Solution

  • Found the reason for the issue in the Troubleshooting section of the official pylance repository:

    PEP 660 enables build backends (ex. setuptools) to use import hooks to direct the import machinery to the package's source files rather than using a .pth file. Import hooks can provide an editable installation that is a more accurate representation of your real installation. However, because resolving module locations using an import hook requires executing Python code, they are not usable by Pylance and other static analysis tools. Therefore, if your editable install is configured to use import hooks, Pylance will be unable to find the corresponding source files.

    If you want to use static analysis tools with an editable install, you should configure the editable install to use .pth files instead of import hooks. See your build backend's documentation for details on how to do this. We have provided some basic information for common build backends below.