How to run python in Visual Studio Code as a main module?
From the command line I would use the -m
switch, like
python -m program.py
I need this to make relative imports work.
Is there something I could add to the launch.json
file?
If this isn't possible, I maybe need to do something with runpy
see python docs, but it would be nice if vscode can do this.
Edit:
For the moment I use, as a workaround, an extra run.py
file which I place outside the package I want to run. Then configure vscode to run that file:
"program": "${workspaceRoot}/../run.py"
From run.py
I import the package and call its entry-point function.
To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.defaultInterpreterPath , which is in the Python extension section of User Settings, with the appropriate interpreter.
Visual Studio Code is a free source code editor that fully supports Python and useful features such as real-time collaboration.
The documentation for debugging a module can be found here: https://code.visualstudio.com/docs/python/debugging#_debugging-specific-app-types
All you need to do is:
Python: Module
debug cofiguration in VS Codelaunch.json
and locate the Python: Module
config section and replace the value for the setting module
with the module name, e.g. program
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