Reputation: 4531
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.
Upvotes: 63
Views: 56842
Reputation: 11
In VS studio code
sys.path.insert("DIRECTORY")
Upvotes: -1
Reputation: 24785
Add this to your settings.json in vscode:
"python.analysis.extraPaths": ["ml/py"],
it can be a relative path based on the root of your project or an absolute path. You can also have several of them.
Upvotes: 0
Reputation: 11
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
.
Upvotes: 0
Reputation: 10609
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"
],
}
Upvotes: 1
Reputation: 37857
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
.
Upvotes: 1
Reputation: 743
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
Upvotes: 6
Reputation: 875
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 :)
Upvotes: 2
Reputation: 1555
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.
<ctrl> + <,>
to open Settings.python.analysis.extraPaths
/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/
Upvotes: 60
Reputation: 5694
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
Upvotes: 32