代码之家  ›  专栏  ›  技术社区  ›  Seph Reed

VSCode“Import X could not be resolve”(导入X无法解析),即使在“help(模块)”下列出`

  •  0
  • Seph Reed  · 技术社区  · 4 年前

    我在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”,它似乎是微软的产品。我怀疑VSCode无法正确整理。可能是因为 pip 在一个意想不到的地方安装了一些东西。这是我的猜测,但任何帮助都将不胜感激。


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

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

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  4
  •   Seph Reed    4 年前

    问题确实在于 Pylance 。它缺少一条“附加路径” pip 已经安装了我想要导入的项目。要解决此问题:

    首先确保您知道导入的位置;您可以通过以下方式找到它:

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

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

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

    你应该准备好了!

    对于另一个示例,您可以看到上面我添加路径的图像 /home/seph/.local/lib/python2.7/ 但无济于事。正在将其更新为 /home/seph/.local/lib/python2.7/site-packages/ 成功了。