如何在调试模式下从VS将命令行参数传递给Python?

时间:2016-02-09 21:27:22

标签: python python-2.7 debugging command-line visual-studio-2015

我正在使用Python Tools for Visual Studio。 (注意,不是IronPython。)

我需要处理从命令行传递给模块的参数。我通过右键单击代码窗口并选择" Start with Debugging"来了解如何在Debug中启动模块。但是这种方法从不提示我输入命令行参数,len(sys.argv)总是== 1。

如何在调试模式下启动模块并将参数传递给它,以便sys.argv有多个成员?

5 个答案:

答案 0 :(得分:7)

  1. 转到项目属性,方法是右键单击项目并选择“属性”或从“项目”菜单中选择“属性”。

  2. 单击Debug,然后在“Script Arguments”字段中输入您的参数。

  3. 保存。

答案 1 :(得分:6)

The steps are shown in the image linked here: https://i.stack.imgur.com/Hx5tf.png

  1. Go to debug mode in VS Code
  2. Click on the settings icon (gear icon). If it does not exist this will create a launch.json
  3. 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

答案 2 :(得分:0)

您想选择"使用Python Interactive执行项目"从调试下拉菜单中。键盘快捷键是Shift + Alt + F5。当您这样做时,您将在屏幕底部打开一个名为Python Interactive的窗口,您将看到打印的语句以及程序输入的任何提示。

这不允许您也进入调试模式。它是一个或另一个。

答案 3 :(得分:0)

我通过在脚本的第一行放置一个断点并打开VS中的立即窗口(您可以在脚本的上下文中执行命令)来解决这个问题。然后我跑

import sys
sys.argv += 'arg1 arg2 --arg3'.split()

答案 4 :(得分:0)

1。如果您正在使用anaconda,请从anaconda导航器中启动vscode。

2。然后从文件夹中打开python文件。

3。然后转到调试选项,然后单击添加配置,然后会启动launch.json文件,该文件中有“控制台”:“ integratedTerminal”行,对此行进行注释并添加“控制台”:“无”并添加“ args”:[“您的命令行参数”]并保存。

4。调试或运行python文件。 见下面的链接。如何在vscode中添加命令行参数? https://code.visualstudio.com/docs/python/debugging

相关问题