Import “Path.to.own.script” could not be resolved Pylance (reportMissingImports) in VS Code using python 3.9x on Lubuntu 20.04

谁说我不能喝 提交于 2021-01-21 09:25:29

问题


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 Lubuntu 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) even though when executing 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.

来源:https://stackoverflow.com/questions/65252074/import-path-to-own-script-could-not-be-resolved-pylance-reportmissingimports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!