I'm just duplicating my answer to this related question here (https://stackoverflow.com/a/75658649/10881599) for anyone else who stumbles across this post in the future:
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.