天天看点

python command line debug,如何在Debug模式下从VS将命令行参数传递给Python?

python command line debug,如何在Debug模式下从VS将命令行参数传递给Python?

I am working with Python Tools for Visual Studio. (Note, not IronPython.)

I need to work with arguments passed to the module from the command line. I see how to start the module in Debug by right-clicking in the code window and selecting "Start with Debugging". But this approach never prompts me for command line arguments, and len(sys.argv) always == 1.

How do I start my module in debug mode and also pass arguments to it so sys.argv has more than 1 member?

解决方案

The steps are shown in the image linked here:

python command line debug,如何在Debug模式下从VS将命令行参数传递给Python?

Go to debug mode in VS Code

Click on the settings icon (gear icon). If it does not exist this will create a launch.json

In the json, in any of the configuration, add the args json parameter:

{

"name": "Python: Terminal (integrated)",

"type": "python",

"request": "launch",

"stopOnEntry": true,

"pythonPath": "${config:python.pythonPath}",

"program": "${file}",

"cwd": "",

"console": "integratedTerminal",

"env": {},

"args": [

"input2.csv",

"output2.csv"

],

"envFile": "${workspaceFolder}/.env",

"debugOptions": [],

"internalConsoleOptions": "neverOpen"

}

Make sure you choose that environment while debugging