'Import "Path.to.own.script" could not be resolved Pylance (reportMissingImports)' in VS Code using Python 3.x on Ubuntu 20.04 LTS
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/
Two methods below:
-
In VS code you can edit the
setting.json
file. If you add"python.analysis.useImportHeuristic": true
the linting error will be removed. -
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
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
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
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 :)
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
.