有没有一种简单的方法在Visual Studio Code中运行Python文件并带有参数?
我知道可以使用args
关键字在launch.json文件中添加自定义配置。但是,每次想使用不同的参数时修改launch.json文件非常麻烦。
有没有一种简单的方法在Visual Studio Code中运行Python文件并带有参数?
我知道可以使用args
关键字在launch.json文件中添加自定义配置。但是,每次想使用不同的参数时修改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"
}
]
}
def main(...)
下的第一行,然后按下F5或Run> 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。它会提示以下弹出窗口。
点击配置构建任务。如果在以下位置.vscode/tasks.json
已经创建了自定义的tasks.json文件,编辑器将打开它。否则,它将提供一个下拉列表,列出已存在的任务运行器。.vscode/tasks.json
。下面是一个tasks.json的示例。
现在编辑 tasks.json 文件以支持Python。
"echo"
更改为"Python"
"Always"
["Hello World"]
更改为["${file}"]
(文件名)problemMatcher
现在,您可以打开您的.py文件并使用快捷键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执行创建的任务,并选择新的“带参数运行Python”任务。
python3 -m debugpy --wait-for-client --listen localhost:5678 myprogram.py
然后,您可以使用一个Python: Remote Attach
启动配置的launch.json连接到程序。这意味着每次打开程序时都需要额外的步骤:在CLI上运行程序,然后附加调试器,但它确实使指定参数更加流畅。
为了使事情变得更简单,您可以将上述内容放入一个bash脚本中,并从CLI传递参数到python:
start.sh "my arg that changes all the time"
然后通过“远程连接”进行附加。
launch.json
中创建配置的人来说,还可以参考xdhmoore的回答,应该如下所示:"configurations": [
...,
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"host": "127.0.0.1",
"port": 5678
}
]
Python: Remote Attach
。我已经寻找了解决此问题的方法,但认为没有任何答案是足够的,因此我创建了一个debugpy-run实用程序来解决它。
如果您已安装VS Code Python扩展,则完整的debugpy调试器已经与其捆绑在一起。该实用程序会查找debugpy安装的路径,然后以监听模式运行指定程序和参数。使用Python“远程附加”调试配置(使用默认主机和端口设置)从VS Code内部连接到它。对于每个调试运行,您可以使用您的shell历史记录和命令行编辑工具,只需control+c
,然后重新运行带有更改参数的命令。
如果您正在使用虚拟环境,请确保使用完整路径来访问该环境的Python解释器。
vscode.exe -path c:\file.py -args a=b
来运行它吗? - Jeremy Thompson