Add Python arguments in script's shebang line (script made with buildout and zc.recipe.egg:scripts)

不羁的心 提交于 2019-12-10 17:49:50

问题


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:


You can force unbuffered I/O from within your Python script by re-opening stdin or stdout by opening a new file object on the filenumber:

import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper
    unbuffered = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True)
except TypeError:
    # Python 2
    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



来源:https://stackoverflow.com/questions/5443080/add-python-arguments-in-scripts-shebang-line-script-made-with-buildout-and-zc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!