How to manage python shebang on mixed systems?

Thomas Passin list1 at tompassin.net
Mon Nov 7 13:34:49 EST 2022


The problem is that some Linux systems - I think - still use Python2 to 
perform various system-related tasks.  When they call "python", they 
need it to be Python2.  If someone has a system like that, they can't 
have the "python" command run Python3.

On 11/7/2022 1:07 PM, Schachner, Joseph (US) wrote:
> Maybe you can't do this, but I would suggest only running on the Python 3 systems.  Refuse to jump through hoops for the Python 2 system.  It is years out of date.
> It is not hard to upgrade from Python 2 to Python 3.  There is a 2to3 utility, and after that there should be very few things you want to manually change.  Perhaps you could encourage them to upgrade.
> 
> --- Joseph S.
> 
> 
> Teledyne Confidential; Commercially Sensitive Business Data
> 
> -----Original Message-----
> From: Chris Green <cl at isbd.net>
> Sent: Monday, November 7, 2022 4:06 AM
> To: python-list at python.org
> Subject: Re: How to manage python shebang on mixed systems?
> 
> 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