【问题标题】:VSCode "Import X could not be resolved" even though listed under `help('modules')`VSCode \"导入 X 无法解析\" 即使列在`help(\'modules\')`
【发布时间】:2023-01-08 01:09:41
【问题描述】:

我在 Python 的第一天,正在尝试将 SciPy 导入到项目中。我通过 pip install 在 ElementaryOS(一个 Ubuntu 衍生产品)上安装了它。我已经通过以下方式验证了它的存在:

$ python
>>> help("modules")

我得到的确切错误是:

导入“scipy”无法解析 Pylance(reportMissingImports)

在搜索此错误时,我发现:

  1. Import could not be resolved/could not be resolved from source Pylance in VS Code using Python 3.9.2 on Windows 10 Powershell——接受的答案都指向一个项目特定的.env文件。我没有这样的项目结构,对我来说也不需要这样的项目结构。

  2. A github issue——这个问题以“它自己修复了”结束

    当我运行我的程序时,我在控制台中没有收到任何错误。并查找“Pylance”,它似乎是 Microsoft 产品。我怀疑 VSCode 无法正确检查。可能是因为 pip 在它不期望的地方安装了一些东西。这是我的猜测,但我们将不胜感激任何帮助。


    编辑:按照缺失路径的想法,我找到了这篇文章——How do I get into the environment VS Code is using for pylance?

    将路径添加到可以找到我的模块的位置没有产生任何结果,但我不确定格式是否正确。也许它需要 glob 语法(例如路径/**/*)

【问题讨论】:

    标签: python visual-studio-code pylance


    【解决方案1】:

    问题确实出在Pylance。它缺少 pip 安装了我要导入的项目的“附加路径”。解决问题:

    首先确保您知道导入的位置;你可以找到它:

    $ python
    >>> import modulename
    >>> print(modulename.__file__)
    

    然后,一旦你知道了位置:

    1. 打开设置 (ctrl + ,)
    2. 搜索“pylance”或在“扩展 > Pylance”下找到它
    3. 找到“额外路径”配置项
    4. 使用“添加项目”添加路径到模块的父文件夹.它不会做任何递归树搜索

      你应该可以开始了!

      再举一个例子,您可以在上图中看到我添加了路径/home/seph/.local/lib/python2.7/,但没有用。将其更新为 /home/seph/.local/lib/python2.7/site-packages/ 就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2022-10-24
      • 2020-05-17
      相关资源
      最近更新 更多