在脚本的 shebang 行中添加 Python 参数(使用 buildout 和 zc.recipe.egg:scripts 制作的脚本) [英] Add Python arguments in script's shebang line (script made with buildout and zc.recipe.egg:scripts)
问题描述
如何在使用 buildout 构建脚本时为 Python 指定参数?
How to specify arguments for Python when building script with buildout?
这是我的 buildout.cfg:
Here's my buildout.cfg:
[buildout]
parts = python
develop = .
[python]
recipe = zc.recipe.egg:scripts
eggs = myproject
和 setup.py:
And setup.py:
from setuptools import setup, find_packages
setup(
name = 'myproject',
packages = find_packages(),
entry_points = """
[console_scripts]
myscript = myproject:main
""",
)
我用这个配置得到以下shebang:
I get the following shebang with this configuration:
$ pip install .
$ head -n1 /usr/local/bin/myscript
#!/usr/bin/python
我想要这个:
#!/usr/bin/python -u
怎么做?我尝试将 arguments = -u
和 interpreter = python -u
添加到 buildout.cfg
.它没有用.
How to do it? I tried adding arguments = -u
and interpreter = python -u
to buildout.cfg
. It didn't work.
推荐答案
您可以通过在文件编号上打开一个新文件对象来重新打开 stdin 或 stdout,从而在 Python 脚本中强制使用无缓冲 I/O:
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)
如果您想使用其他使用 stdout 或 stdin 的模块或内置程序,则可以重新分配 sys.stdout:
You can then reassign sys.stdout if you want to use other modules or build-ins that use stdout or stdin:
sys.stdout = unbuffered
另见 无缓冲标准输出程序中的python(如python -u)
这篇关于在脚本的 shebang 行中添加 Python 参数(使用 buildout 和 zc.recipe.egg:scripts 制作的脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!