Skip to content

3 Ways to disable output buffer when running Python

Question briefing

When using output redirection to run a Python script or placing the Python script into Docker, the output may not be immediately visible on the screen.

python script.py > out.log nohup &

sudo docker run my-python-app

Solution

  1. use python -u script.py
  2. set environment variable, PYTHONUNBUFFERED=1
  3. print('Hello World!', flush=True) in Python > 3.3

Reference

  • https://stackoverflow.com/questions/107705/disable-output-buffering
  • https://stackoverflow.com/questions/3515757/python-print-statements-being-buffered-with-output-redirection
Feedback