Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t获取VSCode/Python调试器以查找我的项目模块_Python_Debugging_Visual Studio Code_Visual Studio Debugging - Fatal编程技术网

Can';t获取VSCode/Python调试器以查找我的项目模块

Can';t获取VSCode/Python调试器以查找我的项目模块,python,debugging,visual-studio-code,visual-studio-debugging,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

我有一个项目,正在调试我的
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
我正在尝试以“调试当前文件-集成终端”的方式进行调试,下面是我的debug settings.json中适用的调试设置。在网上搜索之后,我真的认为在下面添加“/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"
    }

在VS代码中调试Python模块时,我使用模块调试配置,而不是当前文件配置。对您来说,可能是这样的:

{
    "name" : "Python: Module",
    "type" : "python",
    "request": "launch",
    "module": "bbb",
    "args": []
}
请参阅文档

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


调试->添加配置->Python:Module

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

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

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

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

@BrettCannon提到的bug的一个简单解决方法是在
launch.json
配置中添加以下
env
条目:

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

我从VS代码运行调试器。 我在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"
        }
    ]
}

嘿,谢谢你的回答。实际上,我正在调试my
bbb.train
模块(我之前使用的文件是
bbb/train/\uu main\uuuuuuuuuuuuuuuuuuuupy
,但是当我在
Python:module
模式下运行调试器时,我仍然没有得到名为bbb.train的模块。知道为什么吗?当我在调试器似乎所在的同一路径上从外部shell运行
Python-m bbb.train
时,它运行得很好,这让我感到困惑。)…这是一个不使用子包的问题。嗨,Brett,我在两天前通过显式地将pythonpath添加到当前文件夹来运行调试器,正如您在github问题中建议的那样。我昨天刚刚要确认它是否有效,但现在我在VSCode调试器中遇到了这个新问题:仅供参考,这是错误的repo:you want github。com/microsoft/vscode python.path到我的项目文件夹-如果我正在测试子文件夹中的py文件…请在sys.path.append('path to project')之前导入sys然后所有模块导入都工作了。谢谢garyPlease注意,如果使用WSL,所有PYTHONPATH都假定从C:/目录开始,而不是从mnt/目录开始。至少我无法使用mnt使其工作。
“env”:{“PYTHONPATH”:“${workspaceRoot}”}
这是一个很好的解决方法!我不得不这么做很痛苦,但感谢您分享它。
{
    // 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"
        }
    ]
}