简体   繁体   English

如何在 VSC python 代码中重定向输入和 output?

[英]How do I redirect input and output in VSC python code?

I have tried the "<" and ">" commands but vsc still uses the terminal.我已经尝试过“<”和“>”命令,但 vsc 仍然使用终端。

My launch.json:我的launch.json:

"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["<", "${workspaceFolder}input.txt"]

this results in this at the terminal in vsc:这导致在 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'

However, when debugging it still uses the terminal to ask for input.但是,在调试时,它仍然使用终端请求输入。 Same goes if I use ">", "output.txt" .如果我使用">", "output.txt"如此。 The output gets written on the terminal, not in the txt file. output 被写入终端,而不是 txt 文件中。 I have tried to run it manually and it behaves the same way.我试图手动运行它,它的行为方式相同。
I am new to python.我是 python 的新手。 For testing I use the input() function.对于测试,我使用input() function。 I tried using a main() function, maybe the args need to be passed to that but no effect.我尝试使用main() function,也许需要将参数传递给它,但没有效果。 When I was coding in C (in vsc) I had no problem with this.当我在 C(在 vsc 中)中编码时,我对此没有任何问题。 I have tried everything I found on the internet.我已经尝试了我在互联网上找到的所有内容。 Nothing helped.没有任何帮助。 Thank you谢谢

As you have discovered the < is nicely put inside quotes by the ms-python extension and thus the shell will not see it as a redirect.正如您发现的那样,ms-python 扩展很好地将<放在引号内,因此 shell 不会将其视为重定向。

It can be done but requires a bit of configuration.它可以完成,但需要一些配置。

You have to setup the debug client and server yourself.您必须自己设置调试客户端和服务器。

The debug server is setup as a task.调试服务器设置为任务。 The client is an attach launch configuration.客户端是附加启动配置。

First you need to find the location of the debugpy module that is part of the ms-python extension (you can install as a separate module in your environment but why if you already have it).首先,您需要找到作为 ms-python 扩展一部分的debugpy模块的位置(您可以在您的环境中作为单独的模块安装,但如果您已经拥有它,为什么要安装它)。

I use the following 2 files:我使用以下 2 个文件:

redir_input.py

while True:
  try:
    line = input()
  except EOFError:
    break
  print(line)

input.txt

line 1
line 2
line 3

Start a debug session with the redir_input.py file (taking input from the terminal).使用redir_input.py文件(从终端获取输入)开始调试 session。 Terminate the program with Ctrl+Z Enter .使用Ctrl+Z Enter终止程序。

What we want is the command shown on the terminal.我们想要的是终端上显示的命令。 Something like:就像是:

<path>/.venv/Scripts/python <path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy/launcher 64141 -- <path>/redir_input.py

We need the second string: the location of the debugpy module (without the /launcher part)我们需要第二个字符串: debugpy模块的位置(没有/launcher部分)

<path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy

<path> is something specific for your machine. <path>是特定于您的机器的东西。

In .vscode/tasks.json create a task that will start the program to debug:.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": []
    }
  ]
}

Now setup a compound launch config, one starts the debug server and one attaches the debug client.现在设置一个复合启动配置,一个启动调试服务器,一个附加调试客户端。 The debug server launches a dummy python script because we actually need the prelaunchTask .调试服务器启动一个虚拟 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 Debug with input redir as the launch config to run (top of debug bar) and press Run button (little green arrow) or F5 Select使用输入 redir作为要运行的启动配置进行调试(调试栏顶部),然后按运行按钮(绿色小箭头)或F5

The debugger waits till the client has attached ( --wait-for-client ), most likely you want to set a breakpoint on the reading of lines.调试器一直等到客户端附加( --wait-for-client ),很可能您想在读取行时设置断点。

There will be 2 terminals created.将创建 2 个终端。 Select the correct one with the program/task running. Select 正确的一个,程序/任务正在运行。

You can change the names of the task and launch configs.您可以更改任务的名称并启动配置。 Be sure to update the names on multiple locations.请务必更新多个位置的名称。

Thank you, @rioV8 for your reply, I have tried your solution.谢谢@rioV8 的回复,我已经尝试过你的解决方案。 but I ran into some problems: My files.但我遇到了一些问题:我的文件。 launch.json启动.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"
        ]
      }
    ]
  }

tasks.json任务.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": []
      }
    ]
  }

When I debug with Debug with input redir or with Python: Attach to Process port 5678 I get the following error: connect ECONNREFUSED 127.0.0.1:5678 and The preLaunchTask 'Script with redirect input' terminated with exit code 1. .当我使用Debug with input redirPython: Attach to Process port 5678进行调试时,我收到以下错误: connect ECONNREFUSED 127.0.0.1:5678The preLaunchTask 'Script with redirect input' terminated with exit code 1. When I use Python: Start Process port 5678 I only get the latter.当我使用Python: Start Process port 5678我只得到后者。 (exit code 1) Any idea on how to resolve this? (退出代码 1)关于如何解决这个问题的任何想法? Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM