9

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?

0

1 Answer 1

6

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()
Sign up to request clarification or add additional context in comments.

1 Comment

IPython does not seem to be able to read the escape codes correctly when I do this. Things like the tab and arrow keys don't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.