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

问题描述 投票:0回答:8

我有一个项目,正在尝试调试我的

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']

我正在尝试作为“调试当前文件 - 集成终端”进行调试,下面是我的调试 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"
    }
python debugging visual-studio-code visual-studio-debugging
8个回答
57
投票

解决 @BrettCannon 提到的错误的一个简单方法是将以下

env
条目添加到
launch.json
配置中:

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

17
投票

就我而言,我很快就解决了这个问题,选择了正确的口译员:


14
投票

从嵌套目录导入时遇到同样的问题,并通过附加到环境变量 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/"
            }
        }
    ]
}

10
投票

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

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

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

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

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


4
投票

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

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

这里的模块是

src
,位于
assignment
目录下。


2
投票

我从 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"
        }
    ]
}

1
投票

根据其他答案,我必须将 launch.json 更改为以下内容,以便能够成功调试我在项目中编写的任何任意 python 模块(通过点击

F5
开始使用我的
.py
文件进行调试)作为 VSCode 的活动文件)。否则,当文件尝试从不同的自定义模块导入时,我会遇到相同的
"ModuleNotFoundError"

操作系统 = Ubuntu 20.04 (WSL2)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug-my-code",
            "type": "python",
            "python": "/home/<user>/<path/to/my/repo>/.venv/bin/python",
            "request": "launch",
            "program": "${relativeFileDirname}/${fileBasename}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {"PYTHONPATH": "/home/<user>/<path/to/my/repo>"},
        }
    ]
}

注释

  • 我必须将 venv python 的完整绝对路径作为 python 路径 - 即使使用
    ~
    代替
    /home/user/
    也被拒绝
  • 同样,我必须将存储库的完整绝对路径放入 PYTHONPATH 环境变量
  • 对于程序,我需要指定我正在调试的文件相对于存储库根的相对路径。

例如如果我尝试调试的文件是

<repo-root>/src/data/process.py
,那么
"${relativeFileDirname}"
会得到我
src/data
,而
"${fileBasename}"
添加特定模块
process.py

希望这对某人有帮助。我尝试了许多其他组合,但这是唯一最终有效的组合。


0
投票

可能您没有更改调试器的解释。 Click the interpreter and choose the right one

© www.soinside.com 2019 - 2024. All rights reserved.