'导入“Path.to.own.script"在 Ubuntu 20.04 LTS 上使用 Python 3.x 在 VS Code 中无法解决 Pylance (reportMissingImports)' [英] 'Import "Path.to.own.script" could not be resolved Pylance (reportMissingImports)' in VS Code using Python 3.x on Ubuntu 20.04 LTS

查看:59
本文介绍了'导入“Path.to.own.script"在 Ubuntu 20.04 LTS 上使用 Python 3.x 在 VS Code 中无法解决 Pylance (reportMissingImports)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是几个月前使用 我遇到的类似情况.pylint.org/" rel="nofollow noreferrer">pylint 在 pylance 之前>:

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

我的 python 3.9x - 脚本(在 Ubuntu 20.04 LTS 上使用 VS Code)从以下自定义工具"导入开始::

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

现在,Pylance 声明:

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.

因此,为了确保 Pylance 理解这是一个现有的模块路径,除了 sys.path.append(..) - 方法,我将以下内容添加到 settings.json - 文件:

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"
    ],
    ...
}

然而,即使正确导入,我仍然收到 reportMissingImports 消息.

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

我发现了一种解决方法 此处 效果很好(将 # type: ignore 附加到导入语句):

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

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

尽管如此,这只是一种解决方法,这就是我希望解决此问题根源的原因.从技术上讲,这是我之前采用的相同解决方法来消除来自 pylint 的类似警告消息.可能这是 VS-Code settings.json - 配置的固有特性,因为使用 VS-Code 是这里的常数因素.

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.

编辑未解决问题的其他措施:

我添加了

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

到我的 ~/.bashrc - 文件,这使我现在可以从终端直接在 python-shell 中导入模块,而无需之前的 sys-路径操作.然而,这仅适用于全局系统 python 环境,而不适用于任何虚拟环境.为了更改那里的系统路径,我遵循了这些说明,而我的特定虚拟环境scrapy_course"是开放的,就像这样:

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

此命令适用于 virtualenvwrapper,它与 pyenv 整齐.现在,即使在导入模块之前没有 sys.path.append(...) ,我也可以在当前环境中运行我前面提到的脚本,但是 pylance 仍然没有t 正确识别路径并向我显示与以前相同的警告.

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.

编辑"python.analysis.useImportHeuristic":true:

我在全局 settings.json - 文件中不断激活此选项,但我仍然没有注意到任何影响.一旦这种情况发生变化,或者最终有(不同的)解决方案出现在我面前,我会及时通知您.

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.

推荐答案

Pylance 默认包含工作区的根路径.如果您想包含其他子目录作为导入解析路径,您可以使用工作区的 python.analysis.extraPaths 设置添加它们.

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.

  • 在 VS Code 中按 ;+ <,> 打开设置.
  • 输入python.analysis.extraPaths
  • 选择添加项目"
  • 输入库的路径/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/
  • 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/

这篇关于'导入“Path.to.own.script"在 Ubuntu 20.04 LTS 上使用 Python 3.x 在 VS Code 中无法解决 Pylance (reportMissingImports)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆