Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pycharm: read from standard input in "debug" mode

I'm debugging this script in PyCharm:

import sys

def read_in():
    lines = sys.stdin.readlines()
    for i in range(len(lines)):
        lines[i] = lines[i].replace('\n','')
    return lines

def main():
    lines = read_in()
    print lines

if __name__ == '__main__':
    main()

I normally call this script using a command like cat data.txt | python script.py which feeds data.txt into Standard Input.

My question is how can I setup a "Run/Debug Configuration" in PyCharm which feeds data.txt into Standard Input in the same way, but which will allow me to use PyCharm's awesome debugging mode? I suspect that I need to correctly fill out the "Script Parameters" field -- but this has eluded me so far.

Thanks.

like image 339
justinmoon Avatar asked Dec 14 '25 09:12

justinmoon


1 Answers

Personally, I'd just use patch to replace sys.stdin.readlines() with a Mock, with the return value you desire. Place that into a unit test, and run the unit test.

Also, see Reading from a file with sys.stdin in Pycharm

like image 122
user3757614 Avatar answered Dec 15 '25 23:12

user3757614



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!