How to manage python shebang on mixed systems?

Cameron Simpson cs at cskk.id.au
Sun Nov 6 16:56:01 EST 2022


On 06Nov2022 20:51, jak <nospam at please.ty> 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.

I usually use:

     #!/usr/bin/env python3

This runs the default "python3" from my $PATH, whatever that is, 
avoiding a hardwired path to the python3 executable.

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

It is overkill. I generally dislike batch editing scripts.

1: do these scripts work on both python2 and python3? It seems like they 
would need to.
2: write a tiny script _named_ "python3" which invokes python 2. I keep 
a personal "~/bin-local" directory for just such per-system special 
commands like this.
3: with your pseudo "python3" script in place, make all the scripts use 
the "#!/usr/bin/env python3" shebang suggested above.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list