Why Python imports cannot be resolved by Pylance in VS Code, if the package is located in a shared folder?

871 views Asked by At

I am using Pylance extension in VS Code. Some packages import cannot be resolved by Pylance. I found out that these packages are located in a shared folder and the UNC Path of this folder is in the PYTHONPATH (environment variable).

All the non-UNC Path in the PYTHONPATH are correctly treated by Pylance.

The python code is running correctly.

So I want Pylance to correctly treat the packages in the shared folder.

I tried : from mail import Mail

And I have this in Pylance : Import "mail" could not be resolved Pylance(reportMissingImports) [Ln 16, Col 6]

To check that the UNC Path in PYTHONPATH was the issue, I copied the packages and pasted them in a local folder. Then I added the local folder path in PYTHONPATH and Pylance was able to correctly resolve the imports.

2

There are 2 answers

3
MingJie-MSFT On

You can add the following codes to your settings.json to solve this problem:

  "python.analysis.extraPaths": [
    "path/to/your/packages"
  ],
0
Prince Igwe On

I faced the same error some days ago. This is how I fixed it.

  1. Copy the path to your Python interpreter in your virtual environment folder.
  2. Press Ctrl + Shift + P to open command pallete. Choose the Python: Select Interpreter
  3. Choose Enter Interpreter Path
  4. Paste the path you copied. So it should look like ./<Project_Folder>/env/bin/python. Click Enter

And it's solved.