''Import "Path.to.own.script" could not be resolved Pylance (reportMissingImports)' in VS Code using Python 3.x on Ubuntu 20.04 LTS

It is a similar situation I'd encountered several months ago using pylint prior to pylance:

My python 3.9x - script (using VS Code on Ubuntu 20.04 LTS) starts with the following import of custom "tools":

import sys
sys.path.append(
    '/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/'
)

import General.Misc.general_tools as tools

Now, Pylance states:

Import "General.Misc.general_tools" could not be resolvedPylance (reportMissingImports)

This happens even though during the program execution the module is being imported perfectly fine.

Thus, to ensure making Pylance understand that this is an existing module-path, in addition to the sys.path.append(..) - approach, I added the following to the settings.json - file:

{
    ...
    // Possible values: "Jedi", "Pylance", "Microsoft", "None".
    "python.languageServer": "Pylance",
    // NOTE on changing from microsoft to pylance language server: python.autoComplete.extraPaths --> python.analysis.extraPaths
    // Docs: https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
    "python.analysis.extraPaths": [
        "/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts"
    ],
    ...
}

Yet, I still get the reportMissingImports-message even though it's correctly being imported.

A workaround I found here works well (appending # type: ignore to the import-statement):

import General.Misc.general_tools as tools  # type: ignore

Nevertheless, it's just a workaround which is why I'm looking to solve the root of this issue. Technically, it is the same workaround I employed earlier to get rid of similar warning messages from pylint. Probably it's something inherent to the VS-Code settings.json - configuration, since using VS-Code is the constant factor here.


EDIT on additional measures which didn't resolve the problem:

I added

export PYTHONPATH="$PYTHONPATH:/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts"

to my ~/.bashrc - file, which enables me now to import the module directly in a python-shell from terminal without the previous sys-path manipulation. This however applies only to the global system python environment, but not to any virtual environment. In order to change the sys-path there, I followed these instructions, while my particular virtual environment "scrapy_course" is open, like so:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/$ add2virtualenv /home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts

This command applies for virtualenvwrapper, which mananges virtual environments in conjunction with pyenv neatly. Now, I can run my aforementioned script within the current environment even without the sys.path.append(...) prior to import the module, YET pylance still doesn't recognize the paths correctly and shows me the same warning as before.


EDIT on "python.analysis.useImportHeuristic": true:

I've had this option constantly activated in my global settings.json - file and still I didn't notice any effect. I will keep you updated once this should change, or finally a (different) solution crosses my way.


EDIT on suppressing/disabling the Pylance 'reportMissingImports' linting-message:

I've found out how to suppress a specific Pylance-linting-message altogether, if that is of your interest as a workaround. Especially in my current situation, I need to utilize pylint in parallel anyway, so I don't depend on Pylance's linter at all.



Solution 1:[1]

Pylance, by default, includes the root path of your workspace. If you want to include other subdirectories as import resolution paths, you can add them using the python.analysis.extraPaths setting for the workspace.

  • In VS Code press <ctrl> + <,> to open Settings.
  • Type in python.analysis.extraPaths
  • Select "Add Item"
  • Type in the path to your library /home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/

Solution 2:[2]

Two methods below:

  1. In VS code you can edit the setting.json file. If you add "python.analysis.useImportHeuristic": true the linting error will be removed.

  2. The alternative is to add # type: ignore at the end of the import code.

Here is the github link that i got the above resolution from: https://github.com/microsoft/pylance-release/issues/68

It worked for me: python 3.9, VScode, windows10

Solution 3:[3]

By default python selects the global interpreter as the interpreter for any python project you have. So the module/package resolution is in the global context. For any linter you are using to pick up your installed modules you have to ensure you select the correct interpreter

For instance, if you have ever worked with pycharm it does ask you to select the interpreter and create virtual environment with the selected interpreter. For the same, if you start a project in visual studio code It takes the global interpreter as the default interpreter even if you create a virtual environment See this on the bottom left section for the selected interpreter

enter image description here

So how do you ensure Pylance/Pylint/ reads modules installed in the virtual environment created? You need to check for the selected interpreter at the bottom left and if not activated on the virtual environment click on the selected interpreter and vs-code will prompt you to select a default interpreter for the project. Select your current virtual environment and visual studio pylance will read the modules in the current environment. Hope this works for anyone who's struggling with the same

enter image description here

enter image description here

Solution 4:[4]

I was facing similar issue, even after having packages on my system, VS Code Pylance was not able to resolve imports.

In my case I had 2 different versions of python installed (one using anaconda distribution and other directly from python.org)

Fix: Select right python interpreter in VS code. Pylance will stop complaining :)

enter image description here

Solution 5:[5]

Another option is to add an .env file at the root of the vscode project. For example:

.env

PYTHONPATH=src/mydir

You either use a relative path like above, or the full path. The benefit of .env is that it will fix both pylance and pylint in vscode. And it's easier to commit and share on git than .vscode/settings.json.

Solution 6:[6]

Pylance is the default the language server used for python project unless you specify it otherwise. If you get (reportMissingImports) and you are sure the dependency has been successfully installed, it means it's installed somewhere else than Pylance expected. This usually happens if you are working with virtual environements.

Pylance by default selects the system python interpreter for any python project, in this case you need to tell Pylance where to find the virtualenv python interpreter by defining this your vscode settings. Under .vscode/settings.json add the following:

{
    "python.defaultInterpreterPath": "~/.pyenv/versions/3.10.2/envs/<my-virtual-env-name>/bin/python",
    "python.linting.ignorePatterns": [
        "**/site-packages/**/*.py"
    ],
}

Now Pylance will know exactly where to look for the installed dependency. In my case I'm using pyenv but it may be any other path depending on what virtualenv tool you use. If you have other dependencies in other custom location, you can add them by specifying "python.analysis.extraPaths":

{
    "python.defaultInterpreterPath": "~/.pyenv/versions/3.10.2/envs/<my-virtual-env-name>/bin/python",
    "python.linting.ignorePatterns": [
        "**/site-packages/**/*.py"
    ],
    "python.analysis.extraPaths": [
        "my-custom-path-1/python/scripts", 
        "my-custom-path-2/something-else"
    ],
}

Solution 7:[7]

If you are using Anaconda environment, a workaround is to add your personal library path to 'python library path' via the command conda develop path/to/your/module.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 D.L
Solution 3 Felix Orinda
Solution 4 Tarun Kumar
Solution 5 wisbucky
Solution 6 Dhia
Solution 7 LinFelix