我会在launch.json中创建一个调试和发布版本

问题描述 投票:0回答:1

几周以来我一直在寻找问题的解决方案。

我使用 VScode 进行编码,我的 launch.json 文件的主要部分如下:

        ...  
    {
        "name": "Python: Poetry run gui debug",
        "type": "python",
        "request": "launch",
        "python": "${workspaceFolder}/.venv/Scripts/python.exe",
        "program": "gui.py",
        "console": "integratedTerminal",
        "justMyCode": true
    },
    {
        "name": "Python: Poetry run gui release",
        "type": "python",
        "request": "launch",
        "python": "${workspaceFolder}/.venv/Scripts/python.exe",
        "program": "gui.py",
        "args": "-O2",
        "console": "integratedTerminal",
        "justMyCode": true
    },
    ...

但是

"args": "-O2"
行上的“优化”选项未被识别。所以程序总是在调试模式下执行。我尝试了几种方法(我不想写所有的尝试),但结果都是一样的:(

该选项适用于我的代码吗?你能帮我找到答案吗?

python visual-studio-code debugging release
1个回答
0
投票

-O
-OO
选项是 Python 解释器的命令行选项,而不是脚本的参数。

不幸的是,VSCode 的 Python 扩展目前不支持将选项传递给

launch.json
中的 Python 解释器。

您可以创建一个脚本来运行您的

gui.py
:

import os
import subprocess

subprocess.run([os.path.join(os.getcwd(), ".venv", "Scripts", "python.exe"), "-O", "gui.py"])
© www.soinside.com 2019 - 2024. All rights reserved.