pythondebuggingvisual-studio-codevisual-studio-debugging

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


I have a project and am trying to debug my main.py. I am really confused why I am getting the following error from the imports at the top of my file (only) when running the debugger:

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

My project folder structure, as shown by these print statements (as shown by the debugger) confirms my 'bbb' module exists, and has an __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']

I'm trying to debug as "debug current file - integrated terminal", below is the applicable debug settings from my debug settings.json. After searching online, I really thought adding "cwd": "/Users/maxepstein/myproject" below would be my solution but it hasn't helped.

"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File (Integrated Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "cwd": "/Users/maxepstein/myproject"
    }

Solution

  • When I am debugging a Python module in VS Code I use the Module debug configuration instead of the Current File one. For you it might look like this:

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

    See the documentation https://code.visualstudio.com/docs/python/debugging

    Also, in VS Code, these steps will auto-populate these settings for you:

    Debug -> Add Configuration -> Python: Module