Python unbuffered

less than 1 minute read

I came across a new-to-me Python thing today that was astonishingly useful, so thought I’d document it.

If you need to simply and globally turn off buffering for a Python script there are two options.

First, you can add the -u command line option. This is not always an option, especially if you are running Python in a script using #!/usr/bin/env python.

Second, and far more useful for me, is you can set the PYTHONUNBUFFERED environment variable to any non-zero value and it has the same effect.

Reference here for this and many other useful environment variables for Python 2.x, and you can probably guess the Python 3.x URL.