简体   繁体   English

如何使用ipython调试使用stdin的脚本?

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

I've got a python script that takes input on stdin. 我有一个python脚本,它接受stdin的输入。 I'd like to drop into IPython.embed(), like this: 我想放入IPython.embed(),像这样:

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. 问题是IPython使用stdin作为交互式控制台,所以它看到的第一件事就是剩下的管道数据。 Then, the pipe closes and the the terminal exits. 然后,管道关闭,终端退出。

Is there a way to debug a script that uses stdin with ipython? 有没有办法调试使用stdin和ipython的脚本?

You could read your stdin input into a list first, then reset stdin: 您可以先将stdin输入读入列表,然后重置stdin:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM