ash*_*ash 5 python setuptools buildout
我需要在使用构建构建的脚本中使用无缓冲的输出。
-u我的方法是在生成的脚本中指定Python 的标志。
这是我的 buildout.cfg:
[buildout]
parts = python
develop = .
[python]
recipe = zc.recipe.egg:scripts
eggs = myproject
和setup.py:
[buildout]
parts = python
develop = .
[python]
recipe = zc.recipe.egg:scripts
eggs = myproject
我得到以下配置的 shebang:
from setuptools import setup, find_packages
setup(
    name = 'myproject',
    packages = find_packages(),
    entry_points = """
    [console_scripts]
    myscript = myproject:main
    """,
)
我想要这个:
$ pip install .
$ head -n1 /usr/local/bin/myscript
#!/usr/bin/python
怎么做?我尝试添加arguments = -u和interpreter = python -u到buildout.cfg. 它不起作用。
您可以通过在文件号上打开一个新文件对象来重新打开 stdin 或 stdout,从而在 Python 脚本中强制使用无缓冲 I/O:
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)
如果您想使用使用 stdout 或 stdin 的其他模块或内置程序,则可以重新分配 sys.stdout:
sys.stdout = unbuffered
另请参阅程序内 python 中的无缓冲 stdout(如 python -u 中)