[英]How do I redirect input and output in VSC python code?
我已经尝试过“<”和“>”命令,但 vsc 仍然使用终端。
我的launch.json:
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["<", "${workspaceFolder}input.txt"]
这导致在 vsc 的终端:
(.venv) PS C:\Users\domip\Desktop\Python> c:; cd 'c:\Users\domip\Desktop\Python'; & 'c:\Users\domip\Desktop\Python\.venv\Scripts\python.exe' 'c:\Users\domip\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher' '63669' '--' 'c:\Users\domip\Desktop\Python\ea2\fizzbuzz.py' '<' 'C:\Users\domip\Desktop\Pythoninput.txt'
但是,在调试时,它仍然使用终端请求输入。 如果我使用">", "output.txt"
如此。 output 被写入终端,而不是 txt 文件中。 我试图手动运行它,它的行为方式相同。
我是 python 的新手。 对于测试,我使用input()
function。 我尝试使用main()
function,也许需要将参数传递给它,但没有效果。 当我在 C(在 vsc 中)中编码时,我对此没有任何问题。 我已经尝试了我在互联网上找到的所有内容。 没有任何帮助。 谢谢
正如您发现的那样,ms-python 扩展很好地将<
放在引号内,因此 shell 不会将其视为重定向。
它可以完成,但需要一些配置。
您必须自己设置调试客户端和服务器。
调试服务器设置为任务。 客户端是附加启动配置。
首先,您需要找到作为 ms-python 扩展一部分的debugpy
模块的位置(您可以在您的环境中作为单独的模块安装,但如果您已经拥有它,为什么要安装它)。
我使用以下 2 个文件:
redir_input.py
while True:
try:
line = input()
except EOFError:
break
print(line)
input.txt
line 1
line 2
line 3
使用redir_input.py
文件(从终端获取输入)开始调试 session。 使用Ctrl+Z Enter
终止程序。
我们想要的是终端上显示的命令。 就像是:
<path>/.venv/Scripts/python <path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/launcher 64141 -- <path>/redir_input.py
我们需要第二个字符串: debugpy
模块的位置(没有/launcher
部分)
<path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy
<path>
是特定于您的机器的东西。
在.vscode/tasks.json
创建一个将启动程序进行调试的任务:
{
"version": "2.0.0",
"tasks": [
{
"label": "Script with redirect input",
"type": "shell",
"command": "${command:python.interpreterPath} <path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy
--listen 5678 --wait-for-client ${workspaceFolder}/redir_input.py < ${workspaceFolder}/input.txt",
"problemMatcher": []
}
]
}
现在设置一个复合启动配置,一个启动调试服务器,一个附加调试客户端。 调试服务器启动一个虚拟 python 脚本,因为我们实际上需要prelaunchTask
。
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach to Process port 5678",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
},
{
"name": "Python: Start Process port 5678",
"type": "python",
"request": "launch",
"code": "print()",
"preLaunchTask": "Script with redirect input"
}
],
"compounds": [
{
"name": "Debug with input redir",
"configurations": [
"Python: Start Process port 5678",
"Python: Attach to Process port 5678"
]
}
]
}
Select使用输入 redir作为要运行的启动配置进行调试(调试栏顶部),然后按运行按钮(绿色小箭头)或F5
调试器一直等到客户端附加( --wait-for-client
),很可能您想在读取行时设置断点。
将创建 2 个终端。 Select 正确的一个,程序/任务正在运行。
您可以更改任务的名称并启动配置。 请务必更新多个位置的名称。
谢谢@rioV8 的回复,我已经尝试过你的解决方案。 但我遇到了一些问题:我的文件。 启动.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach to Process port 5678",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
},
{
"name": "Python: Start Process port 5678",
"type": "python",
"request": "launch",
"code": "print()",
"preLaunchTask": "Script with redirect input"
}
],
"compounds": [
{
"name": "Debug with input redir",
"configurations": [
"Python: Start Process port 5678",
"Python: Attach to Process port 5678"
]
}
]
}
任务.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Script with redirect input",
"type": "shell",
"command": "${command:python.interpreterPath} c:/User/domip/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy --listen 5678 --wait-for-client ${workspaceFolder}/redir_input.py < ${workspaceFolder}/input.txt",
"problemMatcher": []
}
]
}
当我使用Debug with input redir
或Python: Attach to Process port 5678
进行调试时,我收到以下错误: connect ECONNREFUSED 127.0.0.1:5678
和The preLaunchTask 'Script with redirect input' terminated with exit code 1.
。 当我使用Python: Start Process port 5678
我只得到后者。 (退出代码 1)关于如何解决这个问题的任何想法? 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.