My project structure is as follows:
Project Folder
--setup.py
----Module Folder
------ __init__.py
------ __main__.py
My __main__.py
file contains the entry point to my application and the setup file is configured like this:
from setuptools import setup
setup(name='my_project',
version='0.1.0',
packages=['my_project'],
entry_points={
'console_scripts': [
'my_project= my_project.__main__:main'
]})
This means I can run my code without the debugger attached using:
python -m my_project
I've tried debugging using VS Code by navigating to my __main__.py
file and pressing F5 to run but this doesn't work and throws an exception. How do I configure Visual Studio Code to run this module in debug mode?
Also how do I ensure the program also runs the module and not the file I am looking at when I press F5?
The accepted answer didn't work for me (VSCode 1.49.0), and I got an error message reading: Invalid message: "program", "module", and "code" are mutually exclusive
.
Removing the "program": "${file}",
line solved the issue. I think this makes sense, since defining both a module (with an implied entrypoint) and a file is redundant.
My launch.json
looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Module",
"type": "python",
"request": "launch",
"console": "integratedTerminal",
"module": "my_project",
}
]
}
After some research I've found a solution:
launch.json
file for this project. This will be used to configure VS Code.launch.json
file be created, into it paste this:{
"version": "0.2.0",
"configurations": [
{
"name": "Python Module",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonPath": "${config:python.pythonPath}",
"module": "my_project",
"cwd": "${workspaceRoot}",
}
]
}
I found this code here: https://github.com/DonJayamanne/pythonVSCode/issues/518#issuecomment-260838308
Just using this answer didn't work for me though and I got the error: No module named my_project
but I found this answer: https://github.com/DonJayamanne/pythonVSCode/issues/826
In it the final comment tells you add the following to the config.
"env": {"PYTHONPATH":"${workspaceRoot}"},
This fixes the error and now you can press F5 and your module will be debugged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With