How to run python in Visual Studio Code as a main module
Question:
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.
Answers:
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:
- Select the
Python: Module
debug cofiguration in VS Code
- Edit the
launch.json
and locate the Python: Module
config section and replace the value for the setting module
with the module name, e.g. program
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: app",
"type": "python",
"request": "launch",
"module": "module_name.app",
"console": "integratedTerminal"
}
]
}
Following this answer worked for me
https://stackoverflow.com/a/74002325/21195101
Uses the Command Variable VSCode Extension to set the module name automatically. This way regardless of whether you run the file directly or as an import, it behaves the same way.
{
"name": "Python: As Module",
"type": "python",
"request": "launch",
"module": "${command:extension.commandvariable.file.relativeFileDotsNoExtension}",
"console": "integratedTerminal",
"justMyCode": true
}
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.
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:
- Select the
Python: Module
debug cofiguration in VS Code - Edit the
launch.json
and locate thePython: Module
config section and replace the value for the settingmodule
with the module name, e.g.program
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: app",
"type": "python",
"request": "launch",
"module": "module_name.app",
"console": "integratedTerminal"
}
]
}
Following this answer worked for me
https://stackoverflow.com/a/74002325/21195101
Uses the Command Variable VSCode Extension to set the module name automatically. This way regardless of whether you run the file directly or as an import, it behaves the same way.
{
"name": "Python: As Module",
"type": "python",
"request": "launch",
"module": "${command:extension.commandvariable.file.relativeFileDotsNoExtension}",
"console": "integratedTerminal",
"justMyCode": true
}