【发布时间】:2017-09-28 00:30:18
【问题描述】:
有没有一种简单的方法可以在 Visual Studio Code 中使用参数运行 Python 文件?
我知道我可以使用 args 关键字在 launch.json 文件中添加自定义配置。但是,每次修改launch.json文件很烦人,只是因为我想使用不同的参数。
【问题讨论】:
标签: python debugging visual-studio-code
有没有一种简单的方法可以在 Visual Studio Code 中使用参数运行 Python 文件?
我知道我可以使用 args 关键字在 launch.json 文件中添加自定义配置。但是,每次修改launch.json文件很烦人,只是因为我想使用不同的参数。
【问题讨论】:
标签: python debugging visual-studio-code
Visual Studio Code 仅支持一个 launch.json 文件。但是,它支持两种或多种配置,并且它们出现在左侧菜单/窗格的下拉列表中(而不是“无配置”)。
在DEBUG面板中,要么点击上面红圈的Config按钮,要么点击蓝色链接“create launch.json file”:
点击它,它会创建一个带有调试配置的 launch.json 文件。编辑此文件并以这种密钥对格式添加 args 并为不同的 args 添加多个,包括 Variable Substitution!
{
// 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 with my args",
"type": "python",
"request": "launch",
"program": "${file}",
"args": [
"--username", "Jeremy",
"--account", "Stackoverflow"
],
"console": "integratedTerminal"
},
{
"name": "Python: Current File with UserName arg",
"type": "python",
"request": "launch",
"program": "${file}",
"args": ["${env:USERNAME}"],
"console": "integratedTerminal"
}
]
}
在您的 Python 脚本中放置一个断点,例如在 def main(...) 下的第一行,然后按 F5 或单击 运行菜单 > 开始调试。
【讨论】:
vscode.exe -path c:\file.py -args a=b 运行它吗?
一种解决方法是让您的脚本请求命令行参数(在内部 Visual Studio Code 控制台中)。
这可以通过使用readline 变得更加有用,它允许您执行诸如按向上箭头键循环查看以前的命令(命令历史记录)等操作。一个例子:
import argparse, readline
def main():
# Ask for additional command line arguments if needed (for VSCode)
parser = argparse.ArgumentParser()
parser.add_argument('--interactive', action='store_true', default=False)
(args, rest) = parser.parse_known_args()
if args.interactive:
try: readline.read_history_file()
except: pass
rest += input("Arguments: ").split(" ") # Get input args
try: readline.write_history_file()
except: pass
# Your other script arguments go here
parser.add_argument("-output-dir", default="/out")
# ...
args = parser.parse_args(rest)
print(args)
if __name__ == "__main__":
main()
然后只需将 Visual Studio Code 设置为始终传入 --interactive 参数,即使您设置了断点,您的脚本也会始终请求参数(带有历史记录!)。
【讨论】:
您可以添加自定义任务来执行此操作。这涉及tasks.json。您可以为您的项目(项目文件夹)添加一个默认的 tasks.json 文件。按着这些次序。键盘按下 Ctrl + Shift + B。会弹出如下提示
点击Configure Build Task 如果已经在以下位置创建了自定义tasks.json文件.vscode/tasks.json编辑器会打开它。如果没有,它将给出已经存在的任务运行器的建议下拉列表。
我们的意图是为我们的项目创建一个自定义的tasks.json文件,所以要创建一个我们需要选择Others选项从下拉。检查下面的屏幕截图。
选择Others 选项后,您会看到一个默认的tasks.json 文件将从项目的根目录创建到位置.vscode/tasks.json。以下是 tasks.json 的示例。
现在编辑 tasks.json 文件以支持 Python。
"echo" 更改为 "Python"
"Always"
["Hello World"] 更改为 ["${file}"](文件名)problemMatcher
您现在可以打开 .py 文件并使用快捷键 Ctrl + Shift + B 很好地运行它。
【讨论】:
debug arg1 arg2 之类的东西,它会像python app.py arg1 arg2 一样启动我的脚本。
如果您的项目中没有 task.json 文件,您可以按 Ctrl + Shift + 创建一个新文件B。然后选择显示给您的第一个选项,然后将它们全部替换为以下内容:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Python with argument",
"type": "shell",
"command": "python PROGRAM_NAME.py ARG1 ARG2 ...",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
否则将上述配置添加到您现有的 tasks.json 文件中。
将上述配置中的 PROGRAM_NAME 替换为您的程序名称和 ARG1 ARG2 ... 表明您的具体参数。
毕竟,您可以使用 Ctrl + Shift + B 执行您创建的任务,然后选择新的“Run Python with argument”任务.
【讨论】:
另一种选择是您可以通过命令行从命令行运行您的 python 程序
python3 -m debugpy --wait-for-client --listen localhost:5678 myprogram.py
然后您可以使用Python: Remote Attachlaunch.json 配置连接到程序。这意味着每次打开程序时都需要额外的步骤:在 CLI 上运行程序,然后附加调试器,但这确实使指定参数更加流畅。
为了让事情变得更简单,您可以将上述内容放在 bash 脚本中,然后将 args 从 CLI 传递到 python:
start.sh "my arg that changes all the time"
然后通过“Remote Attach”进行附加。
【讨论】:
如果您使用的是虚拟环境,请务必使用环境的 Python 解释器的完整路径。
【讨论】:
我已经为此处描述的问题寻找解决方案,但认为任何答案都不够,因此我创建了一个 debugpy-run 实用程序来解决它。
如果您安装了 VS Code Python 扩展,则完整的 debugpy 调试器已经与它捆绑在一起。该实用程序会找到安装 debugpy 的路径,然后在侦听模式下为您指定的程序和参数运行它。使用 Python“远程连接”调试配置(使用默认主机和端口设置)从 VS Code 中连接到它。对于每次调试运行,您只需 control+c,然后使用您的 shell 历史记录和命令行编辑工具重新运行带有更改参数的命令。
【讨论】:
另外,对于xdhmoore's answers,对于在launch.json 中创建配置不太熟悉的人,应该如下所示:
"configurations": [
...,
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"host": "127.0.0.1",
"port": 5678
}
]
要开始调试,首先在终端中发出命令行命令,然后从 VS Code 的调试启动菜单中启动(新创建的)启动配置Python: Remote Attach。
【讨论】:
也许这会做你想做的事:在你的 %PATH% 中创建一个指向 python.exe 的 python.bat 文件:
C:\Users\joecoder\AppData\Local\Programs\Python\Python37\python.exe %*
然后使用 Visual Studio Code 终端窗口(您可以从 Visual Studio Code 顶部的“终端”选项卡打开一个新窗口)将目录更改为 .py 文件所在的位置,然后正常运行:
PS C:\Users\joecoder> cd vscode\python
PS C:\Users\joecoder\vscode\python> python test.py 1 2 3
当然,这在 Visual Studio Code 之外运行,因此请务必在编辑后写出更改,并且您必须使用 print() 样式调试。
【讨论】: