无法让VSCode / Python调试器找到我的项目模块

时间:2018-11-13 22:15:08

标签: python debugging visual-studio-code visual-studio-debugging

我有一个项目,正在尝试调试main.py。我真的很困惑,为什么在运行调试器时从文件顶部的导入(仅)中出现以下错误:

Exception has occurred: ModuleNotFoundError
No module named 'bbb'
  File "/Users/maxepstein/myproject/bbb/train/__main__.py", line 8, in <module>
    from bbb.mysubfolder.myfile import myfunction

我的项目文件夹结构,如这些打印语句所示(如调试器所示),确认我的'bbb'模块存在,并且具有__init __。py:

import os
print(os.getcwd())
print(os.listdir())
print(os.listdir('bbb'))

/Users/maxepstein/myproject
['requirements.txt', 'bbb', 'resources', '__init__.py', 'readme.md', 'results', '.gitignore', '.git', '.vscode', 'bbenv']
['config', 'tests', '__init__.py', 'utils', 'predict', 'train']

我正在尝试调试为“调试当前文件-集成终端”,以下是我的debug settings.json中适用的调试设置。在网上搜索后,我真的以为在下面添加"cwd": "/Users/maxepstein/myproject"是我的解决方案,但没有帮助。

"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File (Integrated Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "cwd": "/Users/maxepstein/myproject"
    }

任何帮助或见识将不胜感激。

5 个答案:

答案 0 :(得分:4)

从嵌套目录导入时遇到相同的问题,并通过附加到env变量PYTHONPATH来解决此问题:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}",
            "env": {
                "PYTHONPATH":"${PYTHONPATH}:/home/maxepstein/myproject/"
            }
        }
    ]
}

答案 1 :(得分:3)

@BrettCannon提到的错误的一种简单解决方法是将以下env条目添加到launch.json配置中:

{
    "version": "0.2.0",
    "configurations": [
        {
           "name": "Python: Current File",
           "type": "python",
           "request": "launch",
           "program": "${file}",
           "console": "integratedTerminal",
           "env": { "PYTHONPATH": "${workspaceRoot}"}
        }
    ]
}

答案 2 :(得分:2)

您可以使用当前文件调试配置。在您要导入模块的调试文件中,将要尝试导入的模块的完整路径添加到系统路径中。

sys.path.append('/Users/my_repos/hw/assignment')
import src.network as network

此处的模块为src,位于assignment目录中。

答案 3 :(得分:1)

当我用VS Code调试Python模块时,我使用模块调试配置而不是当前文件。对您来说,它可能像这样:

{
    "name" : "Python: Module",
    "type" : "python",
    "request": "launch",
    "module": "bbb",
    "args": []
}

请参阅文档https://code.visualstudio.com/docs/python/debugging

此外,在VS Code中,这些步骤将自动为您填充以下设置:

调试->添加配置-> Python:模块

答案 4 :(得分:1)

我从 VS Code 运行调试器。 我在 VS 代码中的结构:

myproject
+vscode
+---launch.json
|
+src
+---test/
+------MainTest.py
+---Main.py

拯救我的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}",
            "env": {"PYTHONPATH": "${workspaceRoot}:src"},
            "console": "integratedTerminal"
        }
    ]
}