[英]Visual Studio Code: run Python file with arguments
有没有一种简单的方法可以使用 arguments 在 Visual Studio Code 中运行 Python 文件?
我知道我可以使用args
关键字在launch.json
文件中添加自定义配置。 但是,因为想用不同的arguments,每次都要修改launch.json文件,很烦人。
Visual Studio Code 仅支持一个launch.json文件。 但是,它支持两种或多种配置,它们出现在左侧菜单/窗格的下拉列表中(而不是“无配置”)。
在 DEBUG 窗格中,单击上面红色圈出的Config按钮或单击蓝色链接“create launch.json file” :
单击它,它会创建一个带有调试配置的launch.json文件。 编辑此文件并以这种密钥对格式添加参数,并为不同的参数添加多个参数,包括变量替换!
{
// 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或单击Run Menu > Start Debugging 。
一种解决方法是让您的脚本要求命令行参数(在内部 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文件,因此要创建一个,我们需要从下拉列表中选择其他选项。 检查下面的屏幕截图。
选择其他选项后,您会看到默认的tasks.json文件将从项目的根目录创建到.vscode/tasks.json
位置。 下面是一个tasks.json的例子。
现在编辑tasks.json文件以支持 Python。
"echo"
更改为"Python"
"Always"
["Hello World"]
更改为["${file}"]
(文件名)problemMatcher
您现在可以打开 .py 文件并使用快捷键Ctrl + Shift + B很好地运行它。
如果您的项目中没有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 Attach
launch.json 配置来连接到该程序。 这意味着每次打开程序时都需要一个额外的步骤:在 CLI 上运行程序然后附加调试器,但这确实使指定参数更加流畅。
为了让事情更简单,您可以将上述内容放在 bash 脚本中,然后将 args 从 CLI 传递到 python:
start.sh "my arg that changes all the time"
然后通过“远程连接”进行连接。
如果您使用的是虚拟环境,请务必使用该环境的 Python 解释器的完整路径。
我一直在寻找这里描述的问题的解决方案,但认为任何答案都不够,所以我创建了一个debugpy-run实用程序来解决它。
如果您安装了 VS Code Python 扩展,那么完整的 debugpy 调试器已经与它捆绑在一起。 该实用程序会找到 debugpy 的安装路径,然后在侦听模式下为您指定的程序和参数运行它。 使用 Python“远程连接”调试配置(使用默认主机和端口设置)从 VS Code 中连接到它。 对于每次调试运行,您只需control+c
,然后使用 shell 历史记录和命令行编辑工具重新运行带有更改参数的命令。
此外,对于xdhmoore 的回答,对于那些不太熟悉在launch.json
中创建配置的人来说,它应该如下所示:
"configurations": [
...,
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"host": "127.0.0.1",
"port": 5678
}
]
要开始调试,首先在终端中发出命令行命令,然后从 VS Code 的调试启动菜单中启动(新创建的)启动配置Python: Remote Attach
。
我在 VS 2019 中遇到了类似的问题。我想在 VS 生成的环境中用 arguments 启动 python 文件。 我找不到好的简单解决方案,所以基本上我去了 ENV\Scripts 文件夹,在那里打开终端并运行
python "FullPath\MyFile.py" -some arguments
它工作正常,但需要关闭 VS。
也许这会做你想做的事:在你的%PATH%中创建一个python.bat文件,指向python.exe :
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() 样式调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.