如何为 Python(调试器)设置 Visual Studio 代码 stdin/stdout 重定向?
How to setup Visual Studio Code stdin/stdout redirection for Python (debugger)?
我正在使用 Visual Studio 代码来调试我的 Python 代码。
我想重定向 stdin
中的一个文本文件,并将输出写入另一个文件。
我可以通过 运行 python 直接使用以下语法达到该目的:
python code.py < input.txt > output.txt
有没有办法在调试脚本时允许这样做?如果那不可能,我可以使用该参数为 运行 python 创建配置吗?我尝试在 launch.json
中使用 args
参数,但这些参数都放在引号中,这违背了此重定向的目的。
没有任何内置解决方案,但如果您修改应用程序以将 sys.stdin
和 sys.stdout
替换为您希望被视为 stdin
和 stdout
你可以得到同样的效果。
我能够使用 launch.json 配置文件中的 args 参数实现此目的:
{
// 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: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [ "<", "input.txt",
">", "output.txt" ]
}
]
}
当运行调试器你得到如下命令行:
bash-3.2$ env DEBUGPY_LAUNCHER_PORT=51669 /usr/local/opt/python/bin/python3.7 /Users/XXXX/.vscode/extensions/ms-python.python-2020.4.76186/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher "/Users/XXXX/my_script.py" < input.txt > output.txt
Visual Studio代码1.45
macOS 10.14.6
我正在使用 Visual Studio 代码来调试我的 Python 代码。
我想重定向 stdin
中的一个文本文件,并将输出写入另一个文件。
我可以通过 运行 python 直接使用以下语法达到该目的:
python code.py < input.txt > output.txt
有没有办法在调试脚本时允许这样做?如果那不可能,我可以使用该参数为 运行 python 创建配置吗?我尝试在 launch.json
中使用 args
参数,但这些参数都放在引号中,这违背了此重定向的目的。
没有任何内置解决方案,但如果您修改应用程序以将 sys.stdin
和 sys.stdout
替换为您希望被视为 stdin
和 stdout
你可以得到同样的效果。
我能够使用 launch.json 配置文件中的 args 参数实现此目的:
{
// 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: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [ "<", "input.txt",
">", "output.txt" ]
}
]
}
当运行调试器你得到如下命令行:
bash-3.2$ env DEBUGPY_LAUNCHER_PORT=51669 /usr/local/opt/python/bin/python3.7 /Users/XXXX/.vscode/extensions/ms-python.python-2020.4.76186/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher "/Users/XXXX/my_script.py" < input.txt > output.txt
Visual Studio代码1.45
macOS 10.14.6