How to manage python shebang on mixed systems?

Chris Green cl at isbd.net
Mon Nov 7 04:05:53 EST 2022


Cameron Simpson <cs at cskk.id.au> wrote:
> 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.
> 
Yes, that's probably a good idea, less likely to break than mine.


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

Yes, they do, they're mostly very simple utility scripts for doing
things like changing spaces to underscores in filenames and such.
Just putting 'print' parameters in brackets was all that most of them
needed to work in python 3.


> 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.
> 
Yes, that sounds a good plan to me, thanks Cameron.

-- 
Chris Green
·


More information about the Python-list mailing list