My current job is mainly Python, so there was a steep learning curve. I have done some Python previously, but mainly for writing Sublime Text plugins back in the day.
Over the last few months, I’ve set up my VSCode environment on my work machine. Last night I was wanting to do something on a different machine. I could not remember how to debug Python code inside a virtual environment within VSCode!
I kept getting this error
Exception has occurred: ModuleNotFoundError
No module named 'django'
The error could have been a different module, but for me, it was django
.
I sync all my VSCode settings via my dotfiles, so I could not understand why it worked on my work machine and not my personal one. I did know it was a virtual environment issue though.
After some digging, I found out you have to pick your default interpreter. In VS Code open the Command Palette (⇧ ⌘ P). Then select “Python: Select Interpreter".
This will produce a list of Python installations that you can pick from.
Pick your local virtual environment Python interpreter. For me, this is the one that has a star next to it.
This then allows you to use your debug configuration. For completeness, mine was:
{
// 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: Shell plus",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": ["shell_plus"],
"env": {
"DOMAIN": "localhost:8888",
"CLIENT_DOMAIN": "localhost:8080",
"DYLD_FALLBACK_LIBRARY_PATH": "/opt/homebrew/lib/",
},
"django": true,
"justMyCode": true
}
]
}
That's it. Hopefully, I will remember this next time, or at least know I've written it up here.
Photo by Jonny Gios on Unsplash
Top comments (0)