Max Power
Max Power

Reputation: 8954

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

Upvotes: 52

Views: 67772

Answers (8)

plmk
plmk

Reputation: 2634

In my case, I quickly fixed it selecting the right interpreter: 
( The interpreter is shown at bottom-left or bottom-right, depending on the version. )

( The interpreter is shown at bottom-left or bottom-right, depending on the version. )

interpreter selection

Upvotes: 27

Harrison Liang
Harrison Liang

Reputation: 11

Perhaps you have not changed the interpreter of the debugger. Click on the interpreter and choose the right one:

Click the interpreter and choose the right one

Upvotes: 1

capohugo
capohugo

Reputation: 171

Based on the other answers, I had to change my launch.json to the following to be able to successfully debug any arbitrary python module I'd written in my project (by hitting F5 to start debugging with my .py file as VSCode's active file). Otherwise, I'd run into the same "ModuleNotFoundError" when the file tried to import from a different custom module.
OS = Ubuntu 20.04 (WSL2)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug-my-code",
            "type": "python",
            "python": "/home/<user>/<path/to/my/repo>/.venv/bin/python",
            "request": "launch",
            "program": "${relativeFileDirname}/${fileBasename}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {"PYTHONPATH": "/home/<user>/<path/to/my/repo>"},
        }
    ]
}

Notes

  • I had to put the full absolute path to my venv python as the python path - even using ~ in place of /home/user/ was rejected
  • similarly, I had to put the full absolute path to my repo in the PYTHONPATH environment variable
  • For the program I needed to specify the relative path to the file I was debugging, relative to the repo root.

E.g. if the file I'm trying to debug is <repo-root>/src/data/process.py, then "${relativeFileDirname}" gets me src/data, while "${fileBasename}" adds on the specific module process.py

Hope this helps someone. I tried many other combinations but this was the only one to finally work.

Upvotes: 2

JBSnorro
JBSnorro

Reputation: 6726

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

Upvotes: 17

Gino Ureta
Gino Ureta

Reputation: 51

I run the debugger from VS Code. My structure in VS code:

myproject
+vscode
+---launch.json
|
+src
+---test/
+------MainTest.py
+---Main.py

the launch.json that saved me:

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

Upvotes: 5

James Hirschorn
James Hirschorn

Reputation: 7994

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

Upvotes: 76

gary69
gary69

Reputation: 4230

You can use the current file debug configuration. In the file you're debugging that's importing the modules add the full path to the modules you're trying to import to your system path.

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

The module here is src, located in the assignment directory.

Upvotes: 5

epak96
epak96

Reputation: 401

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

Upvotes: 13

Related Questions