簡體   English   中英

無法讓 VSCode/Python 調試器找到我的項目模塊

[英]Can't get VSCode/Python debugger to find my project modules

我有一個項目,正在嘗試調試我的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"
    }

@BrettCannon 提到的錯誤的一個簡單解決方法是將以下env條目添加到launch.json配置:

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

當我在 VS Code 中調試 Python 模塊時,我使用模塊調試配置而不是當前文件配置。 對你來說,它可能是這樣的:

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

請參閱文檔https://code.visualstudio.com/docs/python/debugging

此外,在 VS Code 中,這些步驟將為您自動填充這些設置:

調試 -> 添加配置 -> Python:模塊

從嵌套目錄導入時遇到同樣的問題,並通過附加到環境變量 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/"
            }
        }
    ]
}

您可以使用當前文件調試配置。 在您正在調試的導入模塊的文件中,將您嘗試導入的模塊的完整路徑添加到系統路徑中。

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

就我而言,我很快就修復了它,選擇了正確的解釋器: 在此處輸入圖片說明

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM