【问题标题】:vscode do not find my custom python packagevscode 找不到我的自定义 python 包
【发布时间】:2020-11-04 21:20:03
【问题描述】:

我是 Windows 上用于 python 开发的 VS Code 的新手,我的 pylint 找不到包。 这是我的项目目录结构。

workspace/    <- This is VS Code workspace (E:\workspace)
  .vscode/
    launch.json
    settings.json    
  project1/
    mypackge/
      __init__.py          <- In here, I wrote: `import mypackage.first_sub_pkg`
      first_sub_pkg/
        __init__.py        <- In here, I wrote: `from .second_sub_pkg.mymodule import MyClass`
        second_sub_pkg/
          __init__.py      <- In here, I wrote: `from .mymodule import MyClass`
          mymodule.py    <- This module has class: `MyClass`
    test_script/
      mytest.py
  project2/
  etc.../

我编写了 mytest.py 脚本代码,如下所示:

from mypackge.first_sub_package import MyClass

我将 C:/Anaconda3/python.exe 用于 python 解释器

当我单击 VS Code 右上角的按钮▷(在终端中运行 Python 文件)时,我收到此错误消息

PS E:\workspace> & c:/Anaconda3/python.exe e:/workspace/project1/test_script/mytest.py
Traceback (most recent call last):
  File "e:/workspace/project1/test_script/mytest.py", line 1, in <module>
    from first_sub_pkg.second_sub_pkg import MyClass
ModuleNotFoundError: No module named 'first_sub_pkg'

另外,我添加了 workspace/.vscode/launch.json 之类的:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "pythonPath": "${command:python.interpreterPath}",
            "env": {
                "PYTHONPATH": "${workspaceFolder};E:/workspace/project1"
            }
        }
    ]
}

还有workspace/.vscode/settings.json之类的:

{
    "python.autoComplete.extraPaths": [
        "E:/workspace",
        "E:/workspace/project1",
        "E:/workspace/project1/first_sub_pkg",
    ],
    "python.pythonPath": "c:/Anaconda3/python.exe",
    "terminal.integrated.shell.windows": "C:/windows/System32/WindowsPowerShell/v1.0/powershell.exe",
    "python.linter": "pyLint",
    "python.linting.pylintPath": "pylint"
}

而我的用户 settings.json 文件是这样的:

{
    "python.autoComplete.extraPaths": [
        "E:/workspace",
        "E:/workspace/project1",
        "E:/workspace/project1/first_sub_pkg",
    ]
}

我已经在 Eclipse + pydev 环境中运行过这个测试脚本,运行没有问题。 但不知何故,VSC 无法导入我的模块。

我似乎是系统路径问题,因为当我运行 python 并将 'E:/workspace/project1' 附加到系统路径 (import sys; sys.path.append('E:/workspace/project1');) 时它运行良好,但我不知道如何解决问题。 (在 Windows 设置中添加系统变量也不起作用)。

我错过了什么?有人请帮助我。我搜索了 2 天,但没有找到。

【问题讨论】:

标签: python visual-studio-code vscode-settings pythonpath pythoninterpreter


【解决方案1】:

这个solotion不适用于mac。

  1. 打开vs代码按ctrl+shift+p

  1. 输入:Python:选择解释器

  2. 选择您的环境并再次运行代码。 (rjz 是我的环境名称)

  3. 如果这不能解决,您需要使用 CMD 通过 conda 或 pip 安装包。在我的情况下,使用 VS 代码终端安装软件包并不能解决问题。

  • 对于某些包,您需要使用 vs code 终端安装,除了 CMD。

【讨论】:

    【解决方案2】:

    解决方案:

    一个:

    在 mytest.py 中更改此语句:“from first_sub_pkg.second_sub_pkg import MyClass”

    到“从 mypackage.first_sub_pkg.second_sub_pkg.third_sub_pkg.mymodule 导入 MyClass”。

    两个:

    将 lanuch.json 中的“env”从“PYTHONPATH”更改为:“${workspaceFolder};E:/workspace/project1”

    到“PYTHONPATH”:“${workspaceFolder};${workspaceFolder}/project1/mypackge”。

    解释:

    Python 只能搜索 PYTHONPATH 中的路径。如果模块嵌套在路径中你需要使用'.'连接文件夹直到指向模块文件。

    【讨论】:

    • 感谢您的友好回答和我的不好。我没有third_sub_pkg,但我面临的问题是一样的。 第一个解决方案:我已经使用了该声明(from mypackage...第二个解决方案:我尝试按照您所说的更改 launch.json 文件中的 PYTHONPATH,但没有任何改变。我的测试脚本仍然无法正常工作。您能否更具体地使用“。”最后一句?
    • 解决方案是分开的,你只需要采取一个解决方案(以防万一)。如果你想导入一个模块,你必须让python解释器知道它应该搜索哪些路径,并且只有这些路径下的包才能被视为一个包。当您尝试 "PYTHONPATH": "${workspaceFolder};E:/workspace/project1" 时,python 解释器将知道搜索这两个路径,这意味着它可以将 mypackage 视为一个包,但它不知道是什么first_sub_pkg 的意思,你要告诉它,它在mypackage的包下,通过'mypackage.first_sub_pkg'。
    【解决方案3】:

    first_sub_pkgmytest.py 文件不在同一目录中。您首先必须上一级到project1/,然后到mypackage/,然后继续进行其余的导入。所以你在mytest.py 做的导入应该是这样的:

    from ..mypakage.first_sub_pkg.second_sub_pkg.third_sub_pkg.mymodule import MyClass
    

    我不知道为什么你有这么多子目录,但你的目录结构很快就会变得非常混乱。 编码时请牢记zen of python

    【讨论】:

    • 我只描述了这个问题需要解释的目录,所以所有的目录都是有意义的。此外,在 eclipseo 或 pycharm ide 中,运行相同的代码没有问题。我猜 eclipse 或 pycharm 以某种方式将我的项目导入系统路径,但 VS Code 没有。我只是想知道我应该如何更改 VS Code 设置来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2019-05-08
    相关资源
    最近更新 更多