1

I'm using Python 3.7 on Jupyter Notebook, and my code has a few print() and a few logging.info() calls at various points. When running the code in Jupyter Notebook, the statements appear in the wrong order, making debugging rather difficult.

Sample code:

import logging
from importlib import reload
reload(logging)  # https://stackoverflow.com/a/21475297
logging.basicConfig(level=logging.DEBUG)

print('AAA')
logging.info('111')
print('BBB')
logging.info('222')

Correct output when running via standard python:

AAA
INFO:root:111
BBB
INFO:root:222

Wrong output when running in Jupyter notebook:

INFO:root:111
INFO:root:222
AAA
BBB

I assume this has something to do with flushing the stdout-buffer, but I don't know how Jupyter handles these things. Using print('xxx', flush=True) results in the reverse order:

AAA
BBB
INFO:root:111
INFO:root:222

How would I make sure the order of printing happens in the right order?

Thank you!

zuiqo
  • 1,149
  • 1
  • 10
  • 23

0 Answers0