如何使用超级用户权限或 root 权限在 Visual Studio 代码中启动 python? (即使用 sudo 命令)
How to launch python in Visual Studio Code with superuser permission or root permission? (i.e. using sudo command)
要从命令行执行我的 python 程序,我使用 sudo python myProgram.py
因为我的程序需要 root 权限。
为了从 Visual Studio 代码 IDE 执行相同的操作,我尝试使用 sudo
命令在 launch.json 文件中为 pythonPath
变量添加前缀,但我得到以下错误:
Error: spawn sudo /usr/local/bin/python3 ENOENT
这是我的任务配置
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "sudo /usr/local/bin/python3",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
通过添加以下配置,现在可以使用 sudo 权限执行
调试选项中的 Sudo 和
"console": "externalTerminal",
这里是完整的配置
{
"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"
]
},
Python 调试配置现在有一个 sudo
选项:
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.
默认是false
,所以你需要把它加到你的launch.json里,设置成true
:
{
"name": "run-python-script-with-sudo",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "/path/to/script.py",
"console": "externalTerminal",
"sudo": true
}
请注意,它将使用您为工作区配置的相同 Python 解释器。要覆盖它并设置不同的 Python 解释器,请添加 python
选项:
To use a different interpreter, specify its path instead in the
python
property of a debug configuration.
要从命令行执行我的 python 程序,我使用 sudo python myProgram.py
因为我的程序需要 root 权限。
为了从 Visual Studio 代码 IDE 执行相同的操作,我尝试使用 sudo
命令在 launch.json 文件中为 pythonPath
变量添加前缀,但我得到以下错误:
Error: spawn sudo /usr/local/bin/python3 ENOENT
这是我的任务配置
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "sudo /usr/local/bin/python3",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
通过添加以下配置,现在可以使用 sudo 权限执行
调试选项中的 Sudo 和
"console": "externalTerminal",
这里是完整的配置
{
"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"
]
},
Python 调试配置现在有一个 sudo
选项:
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.
默认是false
,所以你需要把它加到你的launch.json里,设置成true
:
{
"name": "run-python-script-with-sudo",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "/path/to/script.py",
"console": "externalTerminal",
"sudo": true
}
请注意,它将使用您为工作区配置的相同 Python 解释器。要覆盖它并设置不同的 Python 解释器,请添加 python
选项:
To use a different interpreter, specify its path instead in the
python
property of a debug configuration.