IPython print output is broken

Viewed 360

I installed Jupyter and Ipython in a virtual environment. Whenever I type something in the IPython REPL and press enter, the output is prepended with a bunch of repeated garbage:

In [2]: print("Hello, world!")

line='print("Hello, world!")\n', last_line=b'', encoding=None
print
(
"Hello, world!"
)


line=b'', last_line='print("Hello, world!")\n', encoding=None

line='print("Hello, world!")\n', last_line=b'', encoding=None
print
(
"Hello, world!"
)


line=b'', last_line='print("Hello, world!")\n', encoding=None

line='print("Hello, world!")\n', last_line=b'', encoding=None
print
(
"Hello, world!"
)


line=b'', last_line='print("Hello, world!")\n', encoding=None
Hello, world!
In [3]: 2 + 2
line='2 + 2\n', last_line=b'', encoding=None
2
+
2


line=b'', last_line='2 + 2\n', encoding=None

line='2 + 2\n', last_line=b'', encoding=None
2
+
2


line=b'', last_line='2 + 2\n', encoding=None

line='2 + 2\n', last_line=b'', encoding=None
2
+
2


line=b'', last_line='2 + 2\n', encoding=None

line='2 + 2', last_line=b'', encoding=None
2
+
2
line='', last_line='2 + 2', encoding=None
Out[3]: 4

This garbage output occurs in both IPython REPL in a terminal, and Jupyter notebooks in a browser.

I'm running Windows 10 with Python 3.8.1. The package versions are:

  • jupyter 1.0.0
  • ipython 7.17.0
  • notebook 6.1.1

These were installed via Poetry. I installed the exact same Python and package versions on a separate PC (running Linux) and the problem did not appear, so I suspect something is configured wrong on my Windows PC rather than the problem being a bug in IPython.

How do I make it print like normal again?

1 Answers

There are a few options:

  1. Try to use the following code in your script to see if output of encoding changes. This indicates where the problem may reside.
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

... your code ...

print "something"

  1. If you print using pprint: You may want to try the %pprint command to turn pretty print on or off.

  2. If logging (import logging) is active:

logging.getLogger().setLevel(logging.CRITICAL)

See also: https://docs.python.org/3/library/logging.html

Related