如何修复某些系统无法识别的 shebang 标志
由于某种原因,我访问的 Red Hat Enterprise Server(版本 5.3)上的 shebang 行中无法识别 -O
(优化)标志。在其他系统上,可以毫无问题地识别该标志。
在 OS X 上执行下面的脚本效果很好。可以验证 -O
标志的识别,因为它可以启用(当不存在时)或禁用(当给定时)if __debug__
条件下的任何内容:
#!/usr/bin/env python -O
if __name__ == '__main__':
if __debug__:
print 'lots of debugging output on'
print 'Fin'
在 RHE 上执行相同的脚本系统结果为:
/usr/bin/env: python -O: 没有这样的文件 或目录
如果没有 -O
标志,脚本将在 RHE 系统上正常执行(即,__debug__
内置变量将设置为 True
)。
有没有跨平台的方法来解决这个问题?是否有一种特定于平台的方法来修复 python 解释器的 shebang 行上的标志问题?
编辑:在解释器范围内设置__debug__
变量(不使用shebang标志)的任何其他解决方法也会很有趣。
For some reason, the -O
(optimized) flag is not recognized in the shebang line on a Red Hat Enterprise Server (release 5.3) that I access. On other systems, the flag is recognized without any issue.
Executing the script below on OS X works fine. Recognition of the -O
flag can be verified because it enables (when absent) or disables (when given) anything under the if __debug__
conditional:
#!/usr/bin/env python -O
if __name__ == '__main__':
if __debug__:
print 'lots of debugging output on'
print 'Fin'
Executing the same script on the RHE system result in:
/usr/bin/env: python -O: No such file
or directory
Without the -O
flag, the script executes normally on the RHE system (i.e., the __debug__
built-in variable will be set to True
).
Is there a cross-platform way to fix this issue? Is there even a platform-specific way to fix the issue of flags on the shebang line to the python interpreter?
Edit: Any other workarounds to setting the __debug__
variable (without using shebang flags) interpreter-wide would also be interesting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
某些系统不允许在
#!
样式行上出现多个参数。在任何情况下,“env hack”都不是解决路径问题的官方推荐方法 - 处理此问题的首选方法是让安装重写#!
行以引用/bin/python
、/usr/bin/python
,根据系统情况而定。http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html
Some systems do not allow multiple arguments on a
#!
-style line. The "env hack" is not an officially recommended way of solving the path problem in any case - the preferred way to deal with this is to have the install rewrite the#!
line to refer to/bin/python
,/usr/bin/python
, as appropriate for the system.http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html
如何制作一个小 shell 脚本:
pythono:
然后更改您的脚本以使用:
另请注意,将环境变量
PYTHONOPTIMIZE
设置为非空字符串与使用相同-O
标志。从man python
手册页:How about making a small shell script:
pythono:
Then change your script to use:
Also note that setting the environment variable
PYTHONOPTIMIZE
to a non-empty string is the same as using the-O
flag. From theman python
man page:为了稍微扩展 unutbu 所说的内容,您可以选择在运行时初始化
PYTHONOPTIMIZE
。这适用于所有现代 shell:为了完整性:
To extend slightly what unutbu said, you have the option of initializing
PYTHONOPTIMIZE
at runtime. This works for all modern shells:And for completeness:
请尝试这个:
Please try this:
将 shebang 更改为:
当然,这假设 Python 解释器安装为
/usr/bin/python
;否则,根据需要进行调整。另请参阅此问题和我的回答。
Change the shebang to:
This assumes, of course, that the Python interpreter is installed as
/usr/bin/python
; otherwise, adjust as needed.See also this question and my answer to it.
我相信您使用
#!/usr/bin/env python
的原因是您可以利用PATH
找到您最喜欢的python 版本
代码>.如果情况并非如此,并且您可以使用
/usr/bin/python
(或解释器的其他硬编码路径),那么最直接的解决方案应该是使用#! /usr/bin/python -O
正如 @keith-thompson 建议的但是,假设您想继续依赖
PATH
来查找python
,您将需要调用 shell 来控制命令行:这与 @unutbu 建议的
pythono
解决方案类似,只是它不依赖于PATH
来查找中间脚本,这意味着它的性能稍好一些,而且性能稍好一些。安全的。此解决方案的缺点是混合了两种语言,这意味着您不能简单地说
python myscript.py
来执行将__debug__
设置为True
的代码。如果您希望有时能够在打开-O
的情况下直接通过脚本(例如~/bin/myscript.py
)执行代码,但有时则需要在关闭-O
的调试环境(例如python ~/bin/myscript.py
)中手动运行脚本,那么我相信你最好的选择是使用 Expert像 @ade-yu 提出的技巧或使用简单的@unutbu 建议的pythono
选项。希望这有帮助!
I believe the reason you are using
#!/usr/bin/env python
is so that you can leverage thePATH
to find your favorite version ofpython
.If that is NOT the case and you are fine using
/usr/bin/python
(or some other hard coded path to the interpreter), then the most direct solution should be to use#!/usr/bin/python -O
as suggested by @keith-thompsonHowever, assuming you want to continue relying on
PATH
to findpython
, you will need to invoke a shell in order to get control over the command line:This is similar to the
pythono
solution suggested by @unutbu except that it doesn't rely on thePATH
to find the intermediate script which means it is slightly better performance and slightly more secure.This solution has the drawback of mixing two languages which means you cannot simply say
python myscript.py
to execute your code with__debug__
set toTrue
. If it is your intention to be able to execute the code sometimes directly via the script (e.g.~/bin/myscript.py
) with-O
turned on but other times to run the script by hand in a debugging environment (e.g.python ~/bin/myscript.py
) with-O
turned off, then I believe your best options are to use expert tricks like that proposed by @ade-yu or to use the simplepythono
option suggested by @unutbu.Hope this helps!
只需让你的 shebang 没有旗帜即可;然后,再次执行环境,并将标志传递给定界符。
基本上在文件末尾有终止 EOF。
Just make your shebang without a flag; then, execute the environment again with the flag passing it to a heredoc.
Basically have your terminating EOF at the end of the file.
的事实
#!/usr/bin/env
,您可以利用python
具有与其命令行开关等效的环境变量env
可用于在运行命令之前设置环境变量将它们放在一起,您将得到以下解决方案:
#!/usr/bin/env PYTHONOPTIMIZE=1 python
You要求“跨平台”。这仅适用于 Unix(例如 Linux、OS X 等)。我不能代表 Windows 发言。我希望这也能在 cygwin 下工作,但是 YMMV。
Assuming you want to continue using
#!/usr/bin/env
, you can take advantage of the facts thatpython
has environment variable equivalents for its command-line switchesenv
can be used to set environment variables before running the commandPutting these together, you get the following solution:
#!/usr/bin/env PYTHONOPTIMIZE=1 python
You asked for "cross-platform". This only works for Unix (e.g. Linux, OS X, etc.). I can't speak for Windows. I would expect this to work under
cygwin
as well but YMMV.