Rectangle 27 0

printing How to flush output of Python print?


$ export PYTHONUNBUFFERED=TRUE
$ python -u script.py
$ python -um package.module
>>> from __future__ import print_function
>>> help(print)
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.
>>> print = functools.partial(print, flush=True)
>>> print
functools.partial(<built-in function print>, flush=True)
>>> print('foo')
foo
>>> print('foo', flush=False)
foo
C:\SET PYTHONUNBUFFERED=TRUE
def foo():
    printf = functools.partial(print, flush=True)
    printf('print stuff like this')
from __future__ import print_function
import sys

if sys.version_info[:2] < (3, 3):
    old_print = print
    def print(*args, **kwargs):
        flush = kwargs.pop('flush', False)
        old_print(*args, **kwargs)
        if flush:
            file = kwargs.get('file', sys.stdout)
            # Why might file=None? IDK, but it works for print(i, file=None)
            file.flush() if file is not None else sys.stdout.flush()
import functools
print = functools.partial(print, flush=True)
import sys
print 'delayed output'
sys.stdout.flush()
print('foo', flush=True)
  • Call file.flush() on the output file (we can wrap python 2's print function to do this), for example, sys.stdout
  • Call print with the flush=True argument.
  • apply this to every print function call in the module with a partial function,print = partial(print, flush=True) applied to the module global.
  • apply this to every python process in your environment with PYTHONUNBUFFERED=TRUE (and unset the variable to undo this).
  • apply this to the process with a flag (-u) passed to the interpreter command

Alternatively, you can just call file.flush() after printing, for example, with the print statement in Python 2:

And we can actually override the new default:

Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.

Here's the help on the print function from Python 2.7.12 - note that there is no flush argument:

I think the best option here is to use the -u flag to get unbuffered output.

If you declare it a global in a function, you're changing it on the module's global namespace, so you should just put it in the global namespace, unless that specific behavior is exactly what you want.

If you want to do this inside a function instead of on a module's global scope, you should give it a different name, e.g.:

Note again, this only changes the current global scope, because the print name on the current global scope will overshadow the builtin print function (or dereference the compatibility function, if using Python 2, in that current global scope).

Note that there is internal buffering in file.readlines() and File Objects (for line in sys.stdin) which is not influenced by this option. To work around this, you will want to use file.readline() inside a while 1: loop.

The above compatibility code will cover most uses, but for a much more thorough treatment, see the six module.

They did not backport the flush argument to Python 2.7 So if you're using Python 2 (or less than 3.3), and want code that's compatible with both 2 and 3, may I suggest the following compatibility code. (Note the __future__ import must be at/very "near the top of your module"):

Using Python 3.3 or higher, you can just provide flush=True as a keyword argument to the print function:

We can see it works just like normal:

You can change the default for the print function by using functools.partial on the global scope of a module:

You can get this behavior for all python processes in the environment or environments that inherit from the environment if you set the environment variable to a nonempty string:

e.g., in Linux or OSX:

from the docs:

Note
Rectangle 27 0

printing How to flush output of Python print?


Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.

Since Python 3.3, you can force the normal print() function to flush without the need to use sys.stdout.flush(); just set the "flush" keyword argument to true. From the documentation:

Note
Rectangle 27 0

printing How to flush output of Python print?


$ export PYTHONUNBUFFERED=TRUE
$ python -u script.py
$ python -um package.module
>>> from __future__ import print_function
>>> help(print)
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.
>>> print = functools.partial(print, flush=True)
>>> print
functools.partial(<built-in function print>, flush=True)
>>> print('foo')
foo
>>> print('foo', flush=False)
foo
C:\SET PYTHONUNBUFFERED=TRUE
def foo():
    printf = functools.partial(print, flush=True)
    printf('print stuff like this')
from __future__ import print_function
import sys

if sys.version_info[:2] < (3, 3):
    old_print = print
    def print(*args, **kwargs):
        flush = kwargs.pop('flush', False)
        old_print(*args, **kwargs)
        if flush:
            file = kwargs.get('file', sys.stdout)
            # Why might file=None? IDK, but it works for print(i, file=None)
            file.flush() if file is not None else sys.stdout.flush()
import functools
print = functools.partial(print, flush=True)
import sys
print 'delayed output'
sys.stdout.flush()
print('foo', flush=True)
  • Call file.flush() on the output file (we can wrap python 2's print function to do this), for example, sys.stdout
  • Call print with the flush=True argument.
  • apply this to every print function call in the module with a partial function,print = partial(print, flush=True) applied to the module global.
  • apply this to every python process in your environment with PYTHONUNBUFFERED=TRUE (and unset the variable to undo this).
  • apply this to the process with a flag (-u) passed to the interpreter command

Alternatively, you can just call file.flush() after printing, for example, with the print statement in Python 2:

And we can actually override the new default:

Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.

Here's the help on the print function from Python 2.7.12 - note that there is no flush argument:

I think the best option here is to use the -u flag to get unbuffered output.

If you declare it a global in a function, you're changing it on the module's global namespace, so you should just put it in the global namespace, unless that specific behavior is exactly what you want.

If you want to do this inside a function instead of on a module's global scope, you should give it a different name, e.g.:

Note again, this only changes the current global scope, because the print name on the current global scope will overshadow the builtin print function (or dereference the compatibility function, if using Python 2, in that current global scope).

Note that there is internal buffering in file.readlines() and File Objects (for line in sys.stdin) which is not influenced by this option. To work around this, you will want to use file.readline() inside a while 1: loop.

The above compatibility code will cover most uses, but for a much more thorough treatment, see the six module.

They did not backport the flush argument to Python 2.7 So if you're using Python 2 (or less than 3.3), and want code that's compatible with both 2 and 3, may I suggest the following compatibility code. (Note the __future__ import must be at/very "near the top of your module"):

Using Python 3.3 or higher, you can just provide flush=True as a keyword argument to the print function:

We can see it works just like normal:

You can change the default for the print function by using functools.partial on the global scope of a module:

You can get this behavior for all python processes in the environment or environments that inherit from the environment if you set the environment variable to a nonempty string:

e.g., in Linux or OSX:

from the docs:

Note
Rectangle 27 0

printing How to flush output of Python print?


$ export PYTHONUNBUFFERED=TRUE
$ python -u script.py
$ python -um package.module
>>> from __future__ import print_function
>>> help(print)
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.
>>> print = functools.partial(print, flush=True)
>>> print
functools.partial(<built-in function print>, flush=True)
>>> print('foo')
foo
>>> print('foo', flush=False)
foo
C:\SET PYTHONUNBUFFERED=TRUE
def foo():
    printf = functools.partial(print, flush=True)
    printf('print stuff like this')
from __future__ import print_function
import sys

if sys.version_info[:2] < (3, 3):
    old_print = print
    def print(*args, **kwargs):
        flush = kwargs.pop('flush', False)
        old_print(*args, **kwargs)
        if flush:
            file = kwargs.get('file', sys.stdout)
            # Why might file=None? IDK, but it works for print(i, file=None)
            file.flush() if file is not None else sys.stdout.flush()
import functools
print = functools.partial(print, flush=True)
import sys
print 'delayed output'
sys.stdout.flush()
print('foo', flush=True)
  • Call file.flush() on the output file (we can wrap python 2's print function to do this), for example, sys.stdout
  • Call print with the flush=True argument.
  • apply this to every print function call in the module with a partial function,print = partial(print, flush=True) applied to the module global.
  • apply this to every python process in your environment with PYTHONUNBUFFERED=TRUE (and unset the variable to undo this).
  • apply this to the process with a flag (-u) passed to the interpreter command

Alternatively, you can just call file.flush() after printing, for example, with the print statement in Python 2:

And we can actually override the new default:

Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.

Here's the help on the print function from Python 2.7.12 - note that there is no flush argument:

I think the best option here is to use the -u flag to get unbuffered output.

If you declare it a global in a function, you're changing it on the module's global namespace, so you should just put it in the global namespace, unless that specific behavior is exactly what you want.

If you want to do this inside a function instead of on a module's global scope, you should give it a different name, e.g.:

Note again, this only changes the current global scope, because the print name on the current global scope will overshadow the builtin print function (or dereference the compatibility function, if using Python 2, in that current global scope).

Note that there is internal buffering in file.readlines() and File Objects (for line in sys.stdin) which is not influenced by this option. To work around this, you will want to use file.readline() inside a while 1: loop.

The above compatibility code will cover most uses, but for a much more thorough treatment, see the six module.

They did not backport the flush argument to Python 2.7 So if you're using Python 2 (or less than 3.3), and want code that's compatible with both 2 and 3, may I suggest the following compatibility code. (Note the __future__ import must be at/very "near the top of your module"):

Using Python 3.3 or higher, you can just provide flush=True as a keyword argument to the print function:

We can see it works just like normal:

You can change the default for the print function by using functools.partial on the global scope of a module:

You can get this behavior for all python processes in the environment or environments that inherit from the environment if you set the environment variable to a nonempty string:

e.g., in Linux or OSX:

from the docs:

Note
Rectangle 27 0

printing How to flush output of Python print?


UnsupportedOperation: IOStream has no fileno.
print
stdout.write
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

Also as suggested in this blog one can reopen sys.stdout in unbuffered mode:

Whoops, Python 3 found out. It won't let me execute this piece of code!

Note
Rectangle 27 0

printing How to flush output of Python print?


python -h

-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x see man page for details on internal buffering relating to '-u'

Note
Rectangle 27 0

printing How to flush output of Python print?


python -h

-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x see man page for details on internal buffering relating to '-u'

Note
Rectangle 27 0

printing How to flush output of Python print?


$ export PYTHONUNBUFFERED=TRUE
$ python -u script.py
$ python -um package.module
>>> from __future__ import print_function
>>> help(print)
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.
>>> print = functools.partial(print, flush=True)
>>> print
functools.partial(<built-in function print>, flush=True)
>>> print('foo')
foo
>>> print('foo', flush=False)
foo
C:\SET PYTHONUNBUFFERED=TRUE
def foo():
    printf = functools.partial(print, flush=True)
    printf('print stuff like this')
from __future__ import print_function
import sys

if sys.version_info[:2] < (3, 3):
    old_print = print
    def print(*args, **kwargs):
        flush = kwargs.pop('flush', False)
        old_print(*args, **kwargs)
        if flush:
            file = kwargs.get('file', sys.stdout)
            # Why might file=None? IDK, but it works for print(i, file=None)
            file.flush() if file is not None else sys.stdout.flush()
import functools
print = functools.partial(print, flush=True)
import sys
print 'delayed output'
sys.stdout.flush()
print('foo', flush=True)
  • Call file.flush() on the output file (we can wrap python 2's print function to do this), for example, sys.stdout
  • Call print with the flush=True argument.
  • apply this to every print function call in the module with a partial function,print = partial(print, flush=True) applied to the module global.
  • apply this to every python process in your environment with PYTHONUNBUFFERED=TRUE (and unset the variable to undo this).
  • apply this to the process with a flag (-u) passed to the interpreter command

Alternatively, you can just call file.flush() after printing, for example, with the print statement in Python 2:

And we can actually override the new default:

Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.

Here's the help on the print function from Python 2.7.12 - note that there is no flush argument:

I think the best option here is to use the -u flag to get unbuffered output.

If you declare it a global in a function, you're changing it on the module's global namespace, so you should just put it in the global namespace, unless that specific behavior is exactly what you want.

If you want to do this inside a function instead of on a module's global scope, you should give it a different name, e.g.:

Note again, this only changes the current global scope, because the print name on the current global scope will overshadow the builtin print function (or dereference the compatibility function, if using Python 2, in that current global scope).

Note that there is internal buffering in file.readlines() and File Objects (for line in sys.stdin) which is not influenced by this option. To work around this, you will want to use file.readline() inside a while 1: loop.

The above compatibility code will cover most uses, but for a much more thorough treatment, see the six module.

They did not backport the flush argument to Python 2.7 So if you're using Python 2 (or less than 3.3), and want code that's compatible with both 2 and 3, may I suggest the following compatibility code. (Note the __future__ import must be at/very "near the top of your module"):

Using Python 3.3 or higher, you can just provide flush=True as a keyword argument to the print function:

We can see it works just like normal:

You can change the default for the print function by using functools.partial on the global scope of a module:

You can get this behavior for all python processes in the environment or environments that inherit from the environment if you set the environment variable to a nonempty string:

e.g., in Linux or OSX:

from the docs:

Note
Rectangle 27 0

printing How to flush output of Python print?


$ export PYTHONUNBUFFERED=TRUE
$ python -u script.py
$ python -um package.module
>>> from __future__ import print_function
>>> help(print)
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.
>>> print = functools.partial(print, flush=True)
>>> print
functools.partial(<built-in function print>, flush=True)
>>> print('foo')
foo
>>> print('foo', flush=False)
foo
C:\SET PYTHONUNBUFFERED=TRUE
def foo():
    printf = functools.partial(print, flush=True)
    printf('print stuff like this')
from __future__ import print_function
import sys

if sys.version_info[:2] < (3, 3):
    old_print = print
    def print(*args, **kwargs):
        flush = kwargs.pop('flush', False)
        old_print(*args, **kwargs)
        if flush:
            file = kwargs.get('file', sys.stdout)
            # Why might file=None? IDK, but it works for print(i, file=None)
            file.flush() if file is not None else sys.stdout.flush()
import functools
print = functools.partial(print, flush=True)
import sys
print 'delayed output'
sys.stdout.flush()
print('foo', flush=True)
  • Call file.flush() on the output file (we can wrap python 2's print function to do this), for example, sys.stdout
  • Call print with the flush=True argument.
  • apply this to every print function call in the module with a partial function,print = partial(print, flush=True) applied to the module global.
  • apply this to every python process in your environment with PYTHONUNBUFFERED=TRUE (and unset the variable to undo this).
  • apply this to the process with a flag (-u) passed to the interpreter command

Alternatively, you can just call file.flush() after printing, for example, with the print statement in Python 2:

And we can actually override the new default:

Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.

Here's the help on the print function from Python 2.7.12 - note that there is no flush argument:

I think the best option here is to use the -u flag to get unbuffered output.

If you declare it a global in a function, you're changing it on the module's global namespace, so you should just put it in the global namespace, unless that specific behavior is exactly what you want.

If you want to do this inside a function instead of on a module's global scope, you should give it a different name, e.g.:

Note again, this only changes the current global scope, because the print name on the current global scope will overshadow the builtin print function (or dereference the compatibility function, if using Python 2, in that current global scope).

Note that there is internal buffering in file.readlines() and File Objects (for line in sys.stdin) which is not influenced by this option. To work around this, you will want to use file.readline() inside a while 1: loop.

The above compatibility code will cover most uses, but for a much more thorough treatment, see the six module.

They did not backport the flush argument to Python 2.7 So if you're using Python 2 (or less than 3.3), and want code that's compatible with both 2 and 3, may I suggest the following compatibility code. (Note the __future__ import must be at/very "near the top of your module"):

Using Python 3.3 or higher, you can just provide flush=True as a keyword argument to the print function:

We can see it works just like normal:

You can change the default for the print function by using functools.partial on the global scope of a module:

You can get this behavior for all python processes in the environment or environments that inherit from the environment if you set the environment variable to a nonempty string:

e.g., in Linux or OSX:

from the docs:

Note
Rectangle 27 0

printing How to flush output of Python print?


Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

Print objects to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.

Since Python 3.3, you can force the normal print() function to flush without the need to use sys.stdout.flush(); just set the "flush" keyword argument to true. From the documentation:

Note