简体   繁体   English

Visual Studio Code Python,在launch.json中确定启动了哪个模块

[英]Visual Studio Code Python, determine in launch.json which module is started

I am using Visual Studio Code to program in python. 我正在使用Visual Studio Code在python中编程。 Currently I have two runnable modules I want to start with different arguments, when pressing F5 on each module. 当前,当在每个模块上按F5键时,我想使用不同的参数来启动两个可运行模块。

I specified a launch.json in the following way to pass arguments to my module: 我以以下方式指定了launch.json ,以将参数传递给模块:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Aktuelle Datei",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/Simulation.py",
            "console": "integratedTerminal",
            "args":  ["-iTestInput"]
        }
    ]
}

Every module I start gets passed the argument -iTestInput , so everything is fine until then. 我启动的每个模块都传递了-iTestInput参数,因此在那之前一切都很好。

Now I wanted to specify two configurations for different modules, so I added a second configuration and wanted to specify the program on which it it should use the config: 现在,我想为不同的模块指定两个配置,因此添加了第二个配置,并希望指定应在其上使用config的程序:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Aktuelle Datei",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/Simulation.py",
            "console": "integratedTerminal",
            "args":  ["-iSimulation"]
        },
        {
            "name": "Python: Aktuelle Datei",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/ConvertToData.py",
            "console": "integratedTerminal",
            "args":  ["-iinput"]
        }
    ]
}

So, I want, when I start Simulation.py that the argument -iSimulation gets passed and when I start ConvertToData.py , that -iinput gets passed. 因此,我想在启动Simulation.py时传递-iSimulation参数,而在启动ConvertToData.py时传递-iinput
But now every time Simulation.py starts with the specified argument. 但是现在,每次Simulation.py以指定的参数开头。 I know why this happens (because i specified the name directly and it is the first conifguration). 我知道为什么会这样(因为我直接指定了名称,这是第一个配置)。 I want that my launch.json differentiates between the modules I started. 我希望我的launch.json区分我启动的模块。
Can someone help? 有人可以帮忙吗?

the names of your launch configs are the same 您的启动配置的名称是相同的

Try this launch.json 试试这个launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Simulation",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/Simulation.py",
            "console": "integratedTerminal",
            "args":  ["-iSimulation"]
        },
        {
            "name": "Python: ConvertToData",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/ConvertToData.py",
            "console": "integratedTerminal",
            "args":  ["-iinput"]
        }
    ]
}

Select the one you want from the combo box on the Debug tab and press F5 从“调试”选项卡上的组合框中选择所需的一个,然后按F5


Edit 编辑

Using the extension Command Variable (v0.5.0) you can use a single launch config using 使用扩展命令变量 (v0.5.0),您可以使用单个启动配置

 { "version": "2.0.0", "tasks": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "args" : ["${input:chooseArgs}"] } ], "inputs": [ { "id": "chooseArgs", "type": "command", "command": "extension.commandvariable.file.fileAsKey", "args": { "Simulation.py": "-iSimulation", "ConvertToData.py": "-iinput" } } ] } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM