using stdin in pycharm [duplicate]

2020-08-20 08:25发布

问题:

I have started to use pycharm and i can't seem to

in the command line i use to run the program in this way:

python parse_file.py < test.txt

when parse_file.py can be simple as :

import sys
for line in sys.stdin:
    print line

i cant find in the configuration where to do it i tried typeing something like <test.txt in the script parameters but no luck

I have looked at https://www.jetbrains.com/pycharm/help/run-debug-configuration-python.html

but no luck there also

Thanks

回答1:

PyCharm's run configurations do not support standard input redirection. Please consider changing your script so that it reads its input from a file directly, and not through the standard input redirection.



回答2:

There is solution, but it is not portable and you will have to adapt it to you OS. I describe it for Windows.

  1. In File > Settings > Tools > External tools, create a new configuration, give it a name of your choice (example: cmd input redirected).
  2. At the bottom of the form, for Program type cmd.exe. In Parameters, type /c "$JDKPath$ parse_file.py < test.txt" (Double quotes are important) and configure the Working directory to the folder where your files are stored.
  3. Optionally, you can play with macros (buttons on the right) to replace some hard coded values by a variable equivalent, that will maybe help you to turn your external tool configuration into something reusable.

Now, you can right click on a file in your project, and select "External Tool > cmd input redirected".

The only annoying thing with that process is you will not be able to rerun the external tool with a shortcut while editing your code. To rerun, you have to set the focus on 'Run' tab, then use Ctrl + F5 (or simply click on the green triangle Rerun 'cmd input redirected'

Hope this help, regards