Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
    }
like image 402
Max Power Avatar asked Nov 13 '18 22:11

Max Power


People also ask

Why does Visual Studio not recognize Python module?

The message simply means that VSCode cannot detect the correct path for a Python module. The cause of "Unresolved Import" could be one of the following reason: VSCode is using the wrong Python path. This is often the case if you're running your code against a virtual environment.

How do I run a Python debugger in VS Code?

You can also start the debugger by clicking on the down-arrow next to the run button on the editor, and selecting Debug Python File in Terminal. The debugger will stop at the first line of the file breakpoint. The current line is indicated with a yellow arrow in the left margin.

How do I enable VS Code debugging?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.


2 Answers

A simple workaround to the bug mentioned by @BrettCannon is to add the following env entry to the launch.json configuration:

{
    "version": "0.2.0",
    "configurations": [
        {
           "name": "Python: Current File",
           "type": "python",
           "request": "launch",
           "program": "${file}",
           "console": "integratedTerminal",
           "env": { "PYTHONPATH": "${workspaceRoot}"}
        }
    ]
}
like image 151
James Hirschorn Avatar answered Sep 21 '22 12:09

James Hirschorn


Had the same problem when importing from a nested directory, and fixed it by appending to the env variable 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/"
            }
        }
    ]
}
like image 45
JBSnorro Avatar answered Sep 19 '22 12:09

JBSnorro