# Unbuffered python output

**URL:** <https://forum.ansible.com/t/unbuffered-python-output/14634>\
**Category:** Ansible Developer\
**Created:** [February 22, 2014, 8:30am UTC](https://forum.ansible.com/t/unbuffered-python-output/14634 "2014-02-22T08:30:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Sankauskas](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@Peter\_Sankauskas](https://forum.ansible.com/u/Peter_Sankauskas)\
**Post date:** [February 22, 2014, 8:30am UTC](https://forum.ansible.com/t/unbuffered-python-output/14634/1 "2014-02-22T08:30:51Z")

</div>

The top line of ansible-playbook (and other files in bin) start with this:

> #!/usr/bin/env python

I am wondering if it is only me who wishes they started like this instead

> #!/usr/bin/env python -u

Reason being, Python buffers output by default, and on some systems (or rather, being called from within another script), causes the output to not be displayed task by task, but randomly and in big chucks.

From the python man page:

-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put  
stdin, stdout and stderr in binary mode. Note that there is internal buffering in xreadlines(),  
readlines() and file-object iterators (“for line in sys.stdin”) which is not influenced by this  
option. To work around this, you will want to use “sys.stdin.readline()” inside a “while 1:”  
loop.

Now I have no idea if this is only a Python 2.7 thing, and how compatible it is with 2.6 and 3, but thought I would bring it up.

Thoughts?

---

<div class="post-metadata">

**Author:** ![Till\_Maas](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Till\_Maas](https://forum.ansible.com/u/Till_Maas)\
**Post date:** [February 22, 2014, 10:06am UTC](https://forum.ansible.com/t/unbuffered-python-output/14634/2 "2014-02-22T10:06:42Z")

</div>

I am pretty sure this will not work, you need to use /usr/bin/python -u  
instead, because "python -u" will be passed as a single argument to env  
in your example.

Regards  
Till

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 22, 2014, 1:53pm UTC](https://forum.ansible.com/t/unbuffered-python-output/14634/3 "2014-02-22T13:53:35Z")

</div>

In those other scripts:

export PYTHONUNBUFFERED=true

---

<div class="post-metadata">

**Author:** ![Peter\_Sankauskas](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@Peter\_Sankauskas](https://forum.ansible.com/u/Peter_Sankauskas)\
**Post date:** [February 22, 2014, 6:25pm UTC](https://forum.ansible.com/t/unbuffered-python-output/14634/4 "2014-02-22T18:25:02Z")

</div>

Ah thanks. Will give that a try.
