How to run python in Visual Studio Code as a main module


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

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.


For the moment I use, as a workaround, an extra file which I place outside the package I want to run. Then configure vscode to run that file:

"program": "${workspaceRoot}/../"

From I import the package and call its entry-point function.


The documentation for debugging a module can be found here:

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
Answered By: Don
    "version": "0.2.0",
    "configurations": [
            "name": "Python: app",
            "type": "python",
            "request": "launch",
            "module": "",
            "console": "integratedTerminal"
Answered By: Rodrigo Ibañez

Following this answer worked for me

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
Answered By: Jaxom3
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.