且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pycharm:从“调试”中的标准输入读取模式

更新时间:2023-11-17 23:31:04

个人而言,用一个Mock替换sys.stdin.readlines(),并返回你想要的值。将其放入单元测试中,并运行单元测试。



另请参阅
从Pycharm中的sys.stdin文件读取


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.

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