debugging 无法在python调试器中导入本地文件(vs代码)

n8ghc7c1  于 2022-12-23  发布在  Python
关注(0)|答案(2)|浏览(170)

我正在执行以下操作:
1.第一个月

  1. mkdir folder_structure/utils
  2. touch folder_structure/utils/tools.py
  3. touch folder_structure/main.py
  4. Write in main.py:
from folder_structure.utils.tools import dummy

if __name__ == '__main__':
    dummy()
  1. Write in tools.py:
def dummy():
    print("Dummy")
  1. touch folder_structure/__init__.py
  2. touch folder_structure/utils/__init__.py
    1.运行vs代码调试器
    我得到:
Exception has occurred: ModuleNotFoundError
No module named 'folder_structure'
  File "/Users/davidmasip/Documents/Others/python-scripts/folder_structure/main.py", line 1, in <module>
    from folder_structure.utils.tools import dummy

我的launch.json中包含以下内容:

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

使用调试器时如何导入本地模块?
这对我很有效:

python -m folder_structure.main
1zmg4dgp

1zmg4dgp1#

你需要替换

from folder_structure.utils.tools import dummy

from utils.tools import dummy
zc0qhyus

zc0qhyus2#

将launch.json更改为:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "folder_structure.main",
            "justMyCode": true
        },
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {
                "PYTHONPATH": "${workspaceRoot}"
            },
            "cwd": "${workspaceRoot}",
        }
    ]
}

调试模块工作。

相关问题