简体   繁体   中英

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

I am using Visual Studio Code to program in python. Currently I have two runnable modules I want to start with different arguments, when pressing F5 on each module.

I specified a launch.json in the following way to pass arguments to my module:

{
    // 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.

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:

{
    // 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.
But now every time Simulation.py starts with the specified argument. 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.
Can someone help?

the names of your launch configs are the same

Try this 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


Edit

Using the extension Command Variable (v0.5.0) you can use a single launch config using

 { "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" } } ] } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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