Nick
Nick

Reputation: 5430

How do I print to the console instead of an iPython output cell?

I want to print my output to the console (the one showing the log information or, better yet, a separate console) rather than an output cell in the iPython web notebook.

This is because I frequently have very long output I like to scroll through, and Chrome lags and breaks on large output, whereas iTerm2 does not.

Upvotes: 7

Views: 10889

Answers (2)

Astromax
Astromax

Reputation: 61

Update of the answer from valhallasw: You should use the following, otherwise it returns TypeError: a bytes-like object is required, not 'str'.

import os
os.write(1, b"text\n")

Otherwise, you can also do as Oliver Evans said:

import os
os.write(1, "text\n".encode())

Upvotes: 6

valhallasw
valhallasw

Reputation: 341

You can write to the console by directly writing to file descriptor 1 (instead of sys.stdout, which is mapped to the iPython notebook):

import os
os.write(1, "text\n")

Upvotes: 3

Related Questions