有没有办法在 visual studio 代码中 运行 以管理员身份编码?

Is there a way to run code as administrator in visual studio code?

我有这个问题:Unable to run 'keyboard.is_pressed' on Mac

要解决此问题,我需要从终端以管理员身份 运行 它。有没有办法从 Visual Studio 代码做到这一点?谢谢

我现在也尝试了其他一些到目前为止没有成功的事情。它们是:

请求的消息,最后几行:

Library/Frameworks/Python.framework/Versions/3.9/bin/python3' '/Users/wayow/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/launcher' '59853' '--' '/Users/wayow/mystuff/Pythonstuff/test/problemthing.py'
env: illegal option -- a
usage: env [-iv] [-P utilpath] [-S string] [-u name]
           [name=value ...] [utility [argument ...]]

尝试将其放入 .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current file",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "externalTerminal",
            "sudo": true
        }
    ]
}

和 运行 您的 .py 文件并粘贴您问题中的任何错误消息。

它应该打开一个外部终端。

我在 MacOS Monterey 中测试得很好。

更新

要解决 env '-a' 问题,请打开 Terminal 和 运行 :

  1. mkdir $HOME/bin

  2. Terminal 中粘贴以下内容(来自 cat ...,包括 EOF),最后键入 Enter

cat <<'EOF' > $HOME/bin/env
#!/bin/bash
test "" = -a && shift
exec /usr/bin/env "$@"
EOF
  1. chmod +x "$HOME/bin/env"

  2. PATH="$HOME/bin:$PATH"

  3. open -a "Visual Studio Code" "python-project-directory"

  4. 运行 .py 文件