Add Python arguments in script's shebang line (script made with buildout and zc.recipe.egg:scripts)
Question: How to specify arguments for Python when building script with buildout?

Here's my buildout.cfg:

    
    
    [buildout]
    parts = python
    develop = .
    
    [python]
    recipe = zc.recipe.egg:scripts
    eggs = myproject
    

And setup.py:

    
    
    from setuptools import setup, find_packages
    
    setup(
        name = 'myproject',
        packages = find_packages(),
        entry_points = """
        [console_scripts]
        myscript = myproject:main
        """,
    )
    

I get the following shebang with this configuration:

    
    
    $ pip install .
    $ head -n1 /usr/local/bin/myscript
    #!/usr/bin/python
    

And I want this:

    
    
    #!/usr/bin/python -u
    

How to do it? I tried adding `arguments = -u` and `interpreter = python -u` to
`buildout.cfg`. It didn't work.


Answer: You can force unbuffered I/O from within your Python script by re-opening
stdin or stdout using os.fdopen on the filenumber:

    
    
    import sys, os
    unbuffered = os.fdopen(sys.stdout.fileno(), 'w', 0)
    

You can then reassign sys.stdout if you want to use other modules or build-ins
that use stdout or stdin:

    
    
    sys.stdout = unbuffered
    

Also see [unbuffered stdout in python (as in python -u) from within the
program](http://stackoverflow.com/questions/881696/unbuffered-stdout-in-
python-as-in-python-u-from-within-the-program)

