Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

ipython

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?

like image 942
Reece Avatar asked Dec 20 '12 19:12

Reece


1 Answers

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()
like image 172
tom Avatar answered Nov 04 '22 01:11

tom