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.
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/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With