Print into console terminal not into cell output of IPython Notebook

14.8k views Asked by At

I would like to print into the terminal window that runs IPython Notebook and not into the cell output. Printing into cell output consumes more memory and slows down my system when I issue a substantial number of print calls. In essence, I would like this behaviour by design.

I have tried the following:

  1. I tried a different permutations of print and sys.stdout.write calls
  2. I looked at the IPython Notebook documentation here, here and here without help
  3. I have tried using this as a workaround but it seems to be only working on Python 2.7
3

There are 3 answers

7
MaxPowers On BEST ANSWER

You have to redirect your output to the systems standard output device. This depends on your OS. On Mac that would be:

import sys
sys.stdout = open('/dev/stdout', 'w')

Type the above code in an IPython cell and evaluate it. Afterwards all output will show up in terminal.

2
wxxwxa On

On Windows, this can work:

import sys
sys.stdout = open(1, 'w')
0
fonini On

In order to be able to switch form one to the other easily:

terminal_output = open('/dev/stdout', 'w')

print('this will show up in the IPython cell output')
print('this will show up in the terminal', file=terminal_output)

Similarly, terminal_error = open('/dev/stderr', 'w') can be used to send to the terminal stderr, without any conflict with the default behavior of sys.stderr (which is to print an error message in the IPython cell output).