How to manage python shebang on mixed systems?

Barry barry at barrys-emacs.org
Mon Nov 7 03:24:59 EST 2022




> On 7 Nov 2022, at 03:15, Mike Dewhirst <miked at dewhirst.com.au> wrote:
> 
> On 7/11/2022 6:51 am, jak wrote:
>> Il 06/11/2022 11:03, Chris Green ha scritto:
>>> I have a number of python scripts that I run on a mix of systems.  I
>>> have updated them all to run on python 3 but many will also run quite
>>> happily with python 2.  They all have a #!/usr/bin/python3 shebang.
>>> This works almost everywhere but there is one system where only
>>> python 2 is available (at /usr/bin/python).
>>> I don't have python 2 on any of the systems I manage myself now so a
>>> #!/usr/bin/python shebang will fail.
>>> Is there a neat way of handling this?  I could write a sort of wrapper
>>> script to run via the shebang but that seems overkill to me.
> 
> Can you link the interpreter on each system to the same-named local link and put that in your shebang?
> 
> #!~/scripts/mypython

I do not think ~ works in a #! line. The ~ is handled by the shell, like bash.
But the #! Is handled by the kernel in it exec() handling I recall.

Using /usr/local/bin may be more suitable.

Otherwise you are forced to use the /usr/bin/env method and ensure the PATH includes the command to run.

Barry

> 
>> hi,
>> If you can call Python from the shell prompt, then you could remove the
>> path from shebang:
>> #!python
> 
> 
> -- 
> Signed email is an absolute defence against phishing. This email has
> been signed with my private key. If you import my public key you can
> automatically decrypt my signature and be sure it came from me. Just
> ask and I'll send it to you. Your email software can handle signing.
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list