Can't get VSCode/Python debugger to find my project modules
Solution 1
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}"}
}
]
}
Solution 2
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/"
}
}
]
}
Solution 3
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
Solution 4
In my case, I quickly fixed it selecting the right interpreter:
Solution 5
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.
Related videos on Youtube
Max Power
Some of my Stack Overflow answers: Plot classifier's decision boundary Parallel Processing of Very Large Text File Pretty-print confusion matrix Consistent one-hot-encoding of data in batch Input shape error for hidden layers of Keras LSTM (RNN) Model Hyperparameter Tuning in Pyhon/Scikit-learn Separately Scale/OHE Numeric/Categorical Columns Scipy integral given function, bounds
Updated on July 09, 2022Comments
-
Max Power almost 2 years
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" }
-
Max Power over 5 yearshey thanks for this answer. I'm actually trying to debug a my
bbb.train
module (the file I was in before wasbbb/train/__main__.py
, but am still gettingNo module named bbb.train
when I run the debugger inPython: Module
mode. Any idea why? When I runpython -m bbb.train
from an external shell at the same path the debugger seems to be in, it runs fine, which is confusing me... -
Brett Cannon over 5 yearsThis is a known bug of not working with sub-packages.
-
Max Power over 5 yearsHi Brett, I got the debugger to run two days ago by explicitly adding the pythonpath to current folder as suggested in your github issue. I was just going to confirm that works again yesterday, but now am running into this new issue with the VSCode debugger: github.com/DonJayamanne/pythonVSCode/issues/1441
-
Brett Cannon over 5 yearsFYI that's the wrong repo: you want github.com/microsoft/vscode-python .
-
Abdeali Chandanwala about 4 yearspath to my project folder - if I am testing a py file in a sub folder ... import sys before sys.path.append('path to project') then all Module import worked. Thanks gary
-
asa over 3 yearsPlease note, if one is using WSL, all the PYTHONPATH's are assumed to start in the C:/ directory and not in the mnt/ directory. At least I couldn't make it work using mnt.
-
R.Liu over 3 years
"env": { "PYTHONPATH": "${workspaceRoot}"}
good workaround! -
rsmith54 about 3 yearsIt pains me have to do this, but thank you for sharing it.
-
abk over 2 yearsI was using a virtual environment (.venv folder) and ran into a
ModuleNotFound
error with the debugger. This solved it. -
joaocarlospf over 2 yearsAwesome!! Saved my day. Thanks
-
Melih Dal over 2 yearshow to find launch.jason: on the right panel, find Run&Debug. On the top you will find a drop down menu. Possibly writing "Python: Current File". Press and click on Add Configuration. It will open launch.jason. then follow the answer
-
Dika Arta Karunia about 2 yearsawesome, saved my day, thanks.
-
Alex Sham about 2 yearsthis is the only helpfull answer. **** with init.py didn't help me. Thank you.
-
Mote Zart about 2 yearsI agree @AlexSham and what a nightmare I spent 3+ hours on SO with this problem, including putting my actual python path in that spot as many suggest. Only this exact above syntax solved the problem. To others with this issue:U should at least try this solution even if it seems unlikely to work.