How to debug a script that uses stdin with ipython?

I have a python script that accepts input on stdin. I would like to go to IPython.embed (), for example:

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

Then I invoke the script as follows:

 find . -type f | thescript.py 

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

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

+7
source share
1 answer

First you can read your stdin input in the 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() 
+5
source

All Articles