如何调试将stdin与ipython一起使用的脚本? [英] How do I debug a script that uses stdin with ipython?

查看:48
本文介绍了如何调试将stdin与ipython一起使用的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在stdin上接受输入的python脚本.我想像这样进入IPython.embed():

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

问题在于IPython使用stdin作为交互式控制台,因此它首先看到的是剩余的管道数据.然后,管道关闭,终端退出.

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.

是否可以使用ipython调试使用stdin的脚本?

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

推荐答案

您可以先将stdin输入读入列表,然后重设stdin:

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()

这篇关于如何调试将stdin与ipython一起使用的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆