我有一个项目,正在尝试调试我的
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"
}
解决 @BrettCannon 提到的错误的一个简单方法是将以下
env
条目添加到 launch.json
配置中:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": { "PYTHONPATH": "${workspaceRoot}"}
}
]
}
从嵌套目录导入时遇到同样的问题,并通过附加到环境变量 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/"
}
}
]
}
当我在 VS Code 中调试 Python 模块时,我使用模块调试配置而不是当前文件配置。对于你来说,它可能看起来像这样:
{
"name" : "Python: Module",
"type" : "python",
"request": "launch",
"module": "bbb",
"args": []
}
请参阅文档 https://code.visualstudio.com/docs/python/debugging
此外,在 VS Code 中,这些步骤将为您自动填充这些设置:
调试 -> 添加配置 -> Python:模块
您可以使用当前文件调试配置。在您正在调试的导入模块的文件中,将您尝试导入的模块的完整路径添加到系统路径中。
sys.path.append('/Users/my_repos/hw/assignment')
import src.network as network
这里的模块是
src
,位于assignment
目录下。
我从 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"
}
]
}
根据其他答案,我必须将 launch.json 更改为以下内容,以便能够成功调试我在项目中编写的任何任意 python 模块(通过点击
F5
开始使用我的 .py
文件进行调试)作为 VSCode 的活动文件)。否则,当文件尝试从不同的自定义模块导入时,我会遇到相同的"ModuleNotFoundError"
。
{
"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>"},
}
]
}
注释
~
代替 /home/user/
也被拒绝例如如果我尝试调试的文件是
<repo-root>/src/data/process.py
,那么 "${relativeFileDirname}"
会得到我 src/data
,而 "${fileBasename}"
添加特定模块 process.py
希望这对某人有帮助。我尝试了许多其他组合,但这是唯一最终有效的组合。