如何使用超级用户权限或 root 权限在 Visual Studio Code 中启动 python?(即使用 sudo 命令) [英] How to launch python in Visual Studio Code with superuser permission or root permission? (i.e. using sudo command)

查看:126
本文介绍了如何使用超级用户权限或 root 权限在 Visual Studio Code 中启动 python?(即使用 sudo 命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了从命令行执行我的 python 程序,我使用 sudo python myProgram.py 因为我的程序需要 root 权限.

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

要从 Visual Studio Code IDE 执行相同的操作,我尝试使用 sudo 命令在 launch.json 文件中的 pythonPath 变量前加上前缀,但出现以下错误:

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:

错误:产生 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 权限执行

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

在 debugOptions 和
"console": "externalTerminal",

Sudo in debugOptions and
"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"
        ]
    },

这篇关于如何使用超级用户权限或 root 权限在 Visual Studio Code 中启动 python?(即使用 sudo 命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆