Matt
Matt

Reputation: 2329

Print into console terminal not into cell output of IPython Notebook

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

Upvotes: 16

Views: 15974

Answers (3)

fonini
fonini

Reputation: 3391

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).

Upvotes: 2

wxxwxa
wxxwxa

Reputation: 41

On Windows, this can work:

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

Upvotes: 4

MaxPowers
MaxPowers

Reputation: 5486

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.

Upvotes: 18

Related Questions