Add Python arguments in script's shebang line

2019-06-27 12:54发布

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.

1条回答
爷、活的狠高调
2楼-- · 2019-06-27 13:19

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

查看更多
登录 后发表回答