检测Python正在视觉工作室代码中运行

发布于 2025-01-21 14:11:45 字数 262 浏览 0 评论 0原文

在某些情况下,如果在Visual Studio代码中运行,代码需要采取不同的行动。

有人知道检测Python代码在Visual Studio代码调试器中运行的最有效方法吗?

到目前为止,我能找到的最好的方法是使用:

import sys
if 'debugpy' in sys.modules:
    print("Running in VS Code")

There are cases where code needs to act differently if running in Visual Studio Code.

Does anybody know the most efficient way to detect that the python code is running in the Visual Studio Code debugger?

So far, the best way I could find was using:

import sys
if 'debugpy' in sys.modules:
    print("Running in VS Code")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心不设防 2025-01-28 14:11:45

在vscode上,环境变量term_program设置为“ vscode”,您可以通过:

import os
if 'TERM_PROGRAM' in os.environ.keys() and os.environ['TERM_PROGRAM'] == 'vscode':
    print("Running in VS Code")

On vscode the environment variable TERM_PROGRAM is set to 'vscode', you can check it by:

import os
if 'TERM_PROGRAM' in os.environ.keys() and os.environ['TERM_PROGRAM'] == 'vscode':
    print("Running in VS Code")
江挽川 2025-01-28 14:11:45

我认为解决此问题的最优雅方法只是在Vscode中设置运行任务,该任务运行Python脚本带有额外的命令行标志。

例如:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--vscode', action='store_true')

args = parser.parse_args()

if args.vscode:
    print("vscode")
else:
    print("not vscode")

然后,如果您调用脚本python myScript.py

'不是vscode'

python myScript.py -vscode

'vscode'

然后您只需在VSCODE中添加运行任务:

{
    "label": "run",
    "command": "python", // or python3
    "group": {
        "kind": "test",
        "isDefault": true
    },
    "args": [
        "${file}",
        "--vscode"
    ],
    "presentation": {
        "echo": true,
        "panel": "shared",
        "focus": true
    },
    "problemMatcher": []
}

要运行代码,只需在运行任务中使用快捷方式

I think the most elegant way to solve this is just to set up a run task in vscode that runs the Python script with an extra command line flag.

for example:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--vscode', action='store_true')

args = parser.parse_args()

if args.vscode:
    print("vscode")
else:
    print("not vscode")

then if you call the script python myscript.py

'not vscode'

if you call python myscript.py --vscode

'vscode'

Then you can just add a run task in vscode:

{
    "label": "run",
    "command": "python", // or python3
    "group": {
        "kind": "test",
        "isDefault": true
    },
    "args": [
        "${file}",
        "--vscode"
    ],
    "presentation": {
        "echo": true,
        "panel": "shared",
        "focus": true
    },
    "problemMatcher": []
}

To run your code, just use a shortcut for your run task

东风软 2025-01-28 14:11:45

尽管我认为 @Eran的答案感觉最为简洁,但这是另一种方法:

if ('debugpy' in sys.modules \
       and sys.modules['debugpy'].__file__.find('/.vscode/extensions/') > -1):
    print("Running in VS Code")

它检测debugpy,如果另一个工具使用相同的debugpy namesspace,则会防止误报。

Although I think that @Eran's answer feels the most succinct, here is another method:

if ('debugpy' in sys.modules \
       and sys.modules['debugpy'].__file__.find('/.vscode/extensions/') > -1):
    print("Running in VS Code")

This detects debugpy and prevents false positive if another tool uses the same debugpy namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文