如何修复某些系统无法识别的 shebang 标志

发布于 2024-11-02 07:09:28 字数 923 浏览 6 评论 0原文

由于某种原因,我访问的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

明月松间行 2024-11-09 07:09:28

某些系统不允许在 #! 样式行上出现多个参数。在任何情况下,“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

爱你是孤单的心事 2024-11-09 07:09:28

如何制作一个小 shell 脚本:

pythono

#!/bin/sh    
/usr/bin/env python -O "$@"

然后更改您的脚本以使用:

#!pythono       

另请注意,将环境变量 PYTHONOPTIMIZE 设置为非空字符串与使用相同-O 标志。从 man python 手册页:

   PYTHONOPTIMIZE
          If this is set to a non-empty string it is equivalent to  specifying  the
          -O option. If set to an integer, it is equivalent to specifying -O multi‐
          ple times.

How about making a small shell script:

pythono:

#!/bin/sh    
/usr/bin/env python -O "$@"

Then change your script to use:

#!pythono       

Also note that setting the environment variable PYTHONOPTIMIZE to a non-empty string is the same as using the -O flag. From the man python man page:

   PYTHONOPTIMIZE
          If this is set to a non-empty string it is equivalent to  specifying  the
          -O option. If set to an integer, it is equivalent to specifying -O multi‐
          ple times.
小嗷兮 2024-11-09 07:09:28

为了稍微扩展 unutbu 所说的内容,您可以选择在运行时初始化 PYTHONOPTIMIZE 。这适用于所有现代 shell:

% PYTHONOPTIMIZE=1 foo.py
Fin

为了完整性:

% foo.py
lots of debugging output on
Fin

To extend slightly what unutbu said, you have the option of initializing PYTHONOPTIMIZE at runtime. This works for all modern shells:

% PYTHONOPTIMIZE=1 foo.py
Fin

And for completeness:

% foo.py
lots of debugging output on
Fin
腻橙味 2024-11-09 07:09:28

请尝试这个:

#!/bin/sh
''''exec python -O -- "$0" ${1+"$@"} # '''
if __name__ == '__main__':

    if __debug__:
        print 'lots of debugging output on'

    print 'Fin'
# vi: syntax=python

Please try this:

#!/bin/sh
''''exec python -O -- "$0" ${1+"$@"} # '''
if __name__ == '__main__':

    if __debug__:
        print 'lots of debugging output on'

    print 'Fin'
# vi: syntax=python
方圜几里 2024-11-09 07:09:28

将 shebang 更改为:

#!/usr/bin/python -O

当然,这假设 Python 解释器安装为 /usr/bin/python;否则,根据需要进行调整。

另请参阅此问题我的回答

Change the shebang to:

#!/usr/bin/python -O

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.

一紙繁鸢 2024-11-09 07:09:28

我相信您使用 #!/usr/bin/env python 的原因是您可以利用 PATH 找到您最喜欢的 python 版本代码>.

如果情况并非如此,并且您可以使用 /usr/bin/python (或解释器的其他硬编码路径),那么最直接的解决方案应该是使用 #! /usr/bin/python -O 正如 @keith-thompson 建议的

但是,假设您想继续依赖 PATH 来查找 python,您将需要调用 shell 来控制命令行:

#!/bin/sh
exec python -O - "$@" <<EOF

# ... your python code starts here ...
if __name__ == '__main__':

    if __debug__:
        print 'lots of debugging output on'

    print 'Fin'
# ... your python code ends here ...

EOF

这与 @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 the PATH to find your favorite version of python.

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-thompson

However, assuming you want to continue relying on PATH to find python, you will need to invoke a shell in order to get control over the command line:

#!/bin/sh
exec python -O - "$@" <<EOF

# ... your python code starts here ...
if __name__ == '__main__':

    if __debug__:
        print 'lots of debugging output on'

    print 'Fin'
# ... your python code ends here ...

EOF

This is similar to the pythono solution suggested by @unutbu except that it doesn't rely on the PATH 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 to True. 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 simple pythono option suggested by @unutbu.

Hope this helps!

自控 2024-11-09 07:09:28

只需让你的 shebang 没有旗帜即可;然后,再次执行环境,并将标志传递给定界符。

#!/usr/bin/env python

/usr/bin/env python -O <<EOF
code goes here...
...
EOF

基本上在文件末尾有终止 EOF。

Just make your shebang without a flag; then, execute the environment again with the flag passing it to a heredoc.

#!/usr/bin/env python

/usr/bin/env python -O <<EOF
code goes here...
...
EOF

Basically have your terminating EOF at the end of the file.

天邊彩虹 2024-11-09 07:09:28

的事实

  1. 假设您想继续使用 #!/usr/bin/env,您可以利用python 具有与其命令行开关等效的环境变量
  2. 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 that

  1. python has environment variable equivalents for its command-line switches
  2. env can be used to set environment variables before running the command

Putting 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文