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"
]
}
There is now asudo
option for Python debug configurations:
When set to
true
and used with"console": "externalTerminal"
, allows for debugging apps that require elevation. Using an external console is necessary to capture the password.
It is false
by default, so you need to add it to your launch.json and set it to true
:
{
"name": "run-python-script-with-sudo",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "/path/to/script.py",
"console": "externalTerminal",
"sudo": true
}
Do note that it will use the same Python interpreter you configured for your workspace. To override that and set a different Python interpreter, add the python
option:
To use a different interpreter, specify its path instead in the
python
property of a debug configuration.
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"
]
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.