How do I debug a script that uses stdin with ipython?

你离开我真会死。 提交于 2020-07-06 09:50:44

问题


I've got a python script that takes input on stdin. I'd like to drop into IPython.embed(), like this:

for filepath in sys.stdin:
    dir = os.path.basename(filepath)
    ...
    IPython.embed()

I then invoke the script like this:

find . -type f | thescript.py

The problem is that IPython uses stdin for the interactive console, so the first thing it sees are the remaining pipe data. Then, the pipe closes and the the terminal exits.

Is there a way to debug a script that uses stdin with ipython?


回答1:


You could read your stdin input into a list first, then reset stdin:

stdin_list = list(sys.stdin)
sys.stdin = open('/dev/tty')
for filepath in stdin_list:
    dir = os.path.basename(filepath)
    ...
    IPython.embed()


来源:https://stackoverflow.com/questions/13979422/how-do-i-debug-a-script-that-uses-stdin-with-ipython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!