简体   繁体   中英

Visual Studio Code: How debug Python script with arguments

I'm using Visual Studio Code in order to debug a Python script.

Following this guide , I set up the argument in the launch.json file:

在此处输入图片说明

But when I press on Debug , it says that my argument is not recognized and Visual Studio Code says:

error: unrecognized arguments

在此处输入图片说明

As Visual Studio Code is using PowerShell, let's execute the same file with the same argument:

在此处输入图片说明

So: the same file, same path, and same argument. In the terminal it is working, but not in Visual Studio Code.

Where am I wrong?

I think the --City and Auckland are used as a single argument. Maybe try separating them like so...

Single argument

    "args": ["--city","Auckland"]

Multiple arguments and multiple values

Such as:

--key1 value1 value2 --key2 value3 value4

Just put them into the args list one by one in sequence :

"args": ["--key1", "value1", "value2", "--key2", "value3", "value4"]

In Visual Studio, you can pass multiple parameter as convenient and natural way:

--trail=0 --g=0 --V="HO" --save_interval=10 --verbose=True

I just do not know why they will not support this in Visual Studio Code. To list arguments one by one is cumbersome and a bit silly. They just pass the arguments string to the Python parser, and things can be done easily.

--key1 value1 value2 --key2 value3 value4

can be passed as

"args": ["--key1=value1", "value2", "--key2=value3", "value4"]

(Combining the two answers by Pawan Kumar and Chunde Huang .)

Nobody has mentioned this yet, so I thought I'd offer a suggestion that may save you some minutes and certainly some sanity. I set up my launch.json file with an args array, but I couldn't get my args to show up in the terminal when I ran the debugger.

All I had to do was quit and restart VS Code for some reason. Then it worked like a champ.

File launch.json in the Python project folder path .vscode , tested in Visual Studio Code F5 .

{
    // 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: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": ["c", "pwd"],
        }
    ]
}

I also noticed that if you run the script by clicking on the debug button that looks like this在此处输入图片说明 , then the arguments are not passed. However, using Run -> Start Debugging (or its shortcut F5 ) passed the arguments successfully.

I managed to run in debugging mode thanks to the comment of @Sourya Dey. Thanks.

这也是愚蠢的,但一定要切换回你想要使用启动配置运行的文件,而不是尝试运行launch.json!

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