skip to Main Content

In my Python project I have a module my_stuff.py which is active in my editor, but I want to execute the file main.py to run my project. — Can I use launch.json for this?

I tried a simple config like this:

{
    // 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: Main File",
            "type": "python",
            "request": "launch",
            "program": "main.py",
            "console": "integratedTerminal",
            "justMyCode": true
        },
    ]
}

But this always wants to start the debugpy launcher. I would just like to launch the main file as

/my/venv/python /my/project/dir/main.py

How can I do this with launch.json?

2

Answers


  1. Now you can configure program in launch.json and then use Ctrl+F5 (Run Without Debugging)to execute the program. But essentially this still uses the debugger.

    enter image description here

    There was a feature request to configure the Run button, but it was closed due to insufficient votes.

    There is also a related request open.

    There is also a related SO question.

    Login or Signup to reply.
  2. You can use a task instead of a launch configuration.

    Create the file tasks.json in your .vscode folder and add a task like this:

    {
        "label": "PythonMain",
        "type": "shell",
        "command": "python main.py"
    }
    

    Start it for example via menu Terminal > Run Task....

    You can also create a keyboard shortcut for it.

    This task can also be a prelaunch task in a configuration of your launch.json.

    {
        // 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": [
            {
                ...
                "preLaunchTask": "python main.py",
                ...
            }
        ]
    }
    

    But I think this is not suitable for your use case because it needs to start a launch configuration after it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search