pythonvisual-studio-codesudo

How to launch python in Visual Studio Code with superuser permission or root permission? (i.e. using sudo command)


To execute my python program from the command line, I use sudo python myProgram.py because my program requires root privileges.

To execute the same from Visual Studio Code IDE, I tried prefixing the pythonPath variable in launch.json file with the sudo command but I get the following error:

Error: spawn sudo /usr/local/bin/python3 ENOENT

Here is my task configuration

{
    "name": "Python",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "pythonPath": "sudo /usr/local/bin/python3",
    "program": "${file}",
    "cwd": "${workspaceFolder}",
    "env": {},
    "envFile": "${workspaceFolder}/.env",
    "debugOptions": [
        "RedirectOutput"
    ]
}

Solution

  • By adding the below configuration now am able to execute with sudo privilege

    Sudo in debugOptions and
    "console": "externalTerminal",

    Here is the complete configuration

        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "/usr/local/bin/python3",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "console": "externalTerminal",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput",
                "Sudo"
            ]
        },