0

In windows command line, I am using the redirection operator ("<") to read input from a file. To run my python module, I would do something like:

python myscript.py <input.txt

In myscript.py, I am using sys.stdin.readline() to read the input stream, something like:

def main(argv):
    line = sys.stdin.readline() 
    while line:
        doSomething()
        line = sys.stdin.readline()

if __name__ == "__main__": 
    main(sys.argv)

This works find on the command line.

Is there a way to use the redirection command in IPython? I want to debug the file. Thanks.

I am running Python 3.5.1:: Anaconda 2.5.0 on Win64.

sedeh
  • 7,083
  • 6
  • 48
  • 65

1 Answers1

1

Not easily. The redirection is a feature of the host command shell, and running anything inside IPython will isolate you from that.

Another way to do what you're looking for is to bring IPython into your program. If you know the place where it is breaking, you can add the following code to the except block of a try-except around the broken line:

import IPython
IPython.embed()

This will start an interactive IPython shell in the context the error occurred.

Alternatively, you can run the program under the control of the debugger: Step-by-step debugging with IPython

Community
  • 1
  • 1
mobiusklein
  • 1,403
  • 9
  • 12