Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using stdin in pycharm [duplicate]

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

like image 619
Tomer Avatar asked Feb 26 '15 15:02

Tomer


2 Answers

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.

like image 199
yole Avatar answered Nov 08 '22 10:11

yole


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

like image 26
Antwane Avatar answered Nov 08 '22 10:11

Antwane