Reputation: 8954
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
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. )
Upvotes: 27
Reputation: 11
Perhaps you have not changed the interpreter of the debugger. Click on the interpreter and choose the right one:
Upvotes: 1
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
~
in place of /home/user/
was rejectedE.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
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
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
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
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
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