How to fix shebang flags that are not recognized on some systems

For some reason, the -O flag (optimized) is not recognized on the shebang line on the Red Hat Enterprise Server (release 5.3), which I refer to. In other systems, the flag is recognized without any problems.

Running the script below on OS X works fine. The recognition of the -O flag can be checked because it allows (when absent) or disables (when set) something under the condition if __debug__ :

 #!/usr/bin/env python -O if __name__ == '__main__': if __debug__: print 'lots of debugging output on' print 'Fin' 

Running the same script on the RHE system will result in:

/ usr / bin / env: python -O: No such file or directory

Without the -O flag, the script runs normally on the RHE system (i.e. the __debug__ built-in variable will be set to True ).

Is there a cross-platform way to solve this problem? Is there even a platform-specific way to fix the checkbox problem in the shebang string for the python interpreter?

Edit: Any other workarounds for setting the __debug__ variable (without using the shebang flags) to the interpreter will also be of interest.

+8
python shell shebang
source share
7 answers

How to make a small shell script:

pythono

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

Then change your script to use:

 #!pythono 

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

  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. 
+3
source share

Some systems do not allow multiple arguments in the #! line #! -style. "Env hack" is not an officially recommended way to solve the path problem anyway - the preferred way to deal with this is to have the installation rewrite the line #! to indicate /bin/python , /usr/bin/python , as it is suitable for the system.

http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html

+4
source share

To expand the unutbu value a bit , you have the option to initialize 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 
+4
source share

Please try the following:

 #!/bin/sh ''''exec python -O -- "$0" ${1+"$@"} # ''' if __name__ == '__main__': if __debug__: print 'lots of debugging output on' print 'Fin' # vi: syntax=python 
+1
source share

Change shebang to:

 #!/usr/bin/python -O 

This assumes, of course, that the Python interpreter is installed as /usr/bin/python ; otherwise adjust as necessary.

See also this question and my answer to it .

0
source share

Assuming you want to continue using #!/usr/bin/env , you can take advantage of the facts that

  • python has environment equivalents for its command line switches
  • env can be used to set environment variables before running the command

Combining this, you get the following solution:

#!/usr/bin/env PYTHONOPTIMIZE=1 python

You asked for a cross platform. This only works for Unix (e.g. Linux, OS X, etc.). I can not speak for Windows. I expect this to work under cygwin , but YMMV.

-one
source share

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

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

Basically, your trailing EOF at the end of the file.

-one
source share

All Articles