• How to manage python shebang on mixed systems?

    From Chris Green@21:1/5 to All on Sun Nov 6 10:03:50 2022
    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.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From rbowman@21:1/5 to Chris Green on Sun Nov 6 18:16:39 2022
    On Sun, 6 Nov 2022 10:03:50 +0000, Chris Green wrote:


    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 symlink?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jak@21:1/5 to All on Sun Nov 6 20:51:10 2022
    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.


    hi,
    If you can call Python from the shell prompt, then you could remove the
    path from shebang:

    #!python

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to rbowman on Sun Nov 6 19:31:06 2022
    rbowman <bowman@montana.com> wrote:
    On Sun, 6 Nov 2022 10:03:50 +0000, Chris Green wrote:


    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 symlink?

    Not really, since the system where there's no python3 is one where I
    only have user access.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to jak on Mon Nov 7 08:56:01 2022
    On 06Nov2022 20:51, jak <nospam@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@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mike Dewhirst@21:1/5 to jak on Mon Nov 7 14:02:44 2022
    To: python-list@python.org

    This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------------YznEaGzKDgahGlm3zXdpt50a
    Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: base64

    T24gNy8xMS8yMDIyIDY6NTEgYW0sIGphayB3cm90ZToNCj4gSWwgMDYvMTEvMjAyMiAxMTow MywgQ2hyaXMgR3JlZW4gaGEgc2NyaXR0bzoNCj4+IEkgaGF2ZSBhIG51bWJlciBvZiBweXRo b24gc2NyaXB0cyB0aGF0IEkgcnVuIG9uIGEgbWl4IG9mIHN5c3RlbXMuwqAgSQ0KPj4gaGF2 ZSB1cGRhdGVkIHRoZW0gYWxsIHRvIHJ1biBvbiBwe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YnNvbHV0ZSBkZWZlbmNlIGFnYWluc3QgcGhpc2hpbmcuIFRoaXMgZW1haWwgaGFz DQpiZWVuIHNpZ25lZCB3aXRoIG15IHByaXZhdGUga2V5LiBJZiB5b3UgaW1wb3J0IG15IHB1 YmxpYyBrZXkgeW91IGNhbg0KYXV0b21hdGljYWxseSBkZWNyeXB0IG15IHNpZ25hdHVyZSBh bmQgYmUgc3VyZSBpdCBjYW1lIGZyb20gbWUuIEp1c3QNCmFzayBhbmQgSSdsbCBzZW5kIGl0 IHRvIHlvdS4gWW91ciBlbWFpbCBzb2Z0d2FyZSBjYW4gaGFuZGxlIHNpZ25pbmcuDQoNCg==

    --------------YznEaGzKDgahGlm3zXdpt50a--

    -----BEGIN PGP SIGNATURE-----

    wsB5BAABCAAjFiEE/NCg7Xf1UydoVFgpGvW31BqCOLMFAmNodVQFAwAAAAAACgkQGvW31BqCOLNT WQf8DPBpJhIN9In9BbNqjeptsGHTVOy3dyTsGRDvJz+XqPElNVrU+UlPKeNG9OyjSbep2Yzur7PI B1uG9fWchYElkL1sKV75l6l4cAkb1NwNrUAmxfZvgbV2/s3Ll8XzPLAn3i+K49cEvcYxYWzMPuNj mETv3Nq9/oZHkvRp2h4qbrZcMTRYG1nztk4muUGnZ8MfptkEN5hETAXRAym7dYbRvwkEkNIGboo7 7v+wPqa1yXjOblW6ltjM169fkPjENEYmRkDOfH5EdJ5lQgmWvE5Iavymd0fHOwHGPztm/o59IWYz zwXE9Kpvf+Xs106HEEBxD3pfktDASEMOguj/LwKgkw==
    =R1TA
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Weatherby,Gerard@21:1/5 to All on Mon Nov 7 02:24:36 2022
    A possible solution is here.

    https://superuser.com/questions/815323/can-i-have-a-conditional-shebang ________________________________
    From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of jak <nospam@please.ty>
    Sent: Sunday, November 6, 2022 2:51:10 PM
    To: python-list@python.org <python-list@python.org>
    Subject: Re: How to manage python shebang on mixed systems?

    *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

    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.


    hi,
    If you can call Python from the shell prompt, then you could remove the
    path from shebang:

    #!python

    -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!ke_gwJrnkkPx4fdk8CkLm6Qd2lmYIl7st4qz7Mmn0G8BerBOEwRWBfm51eFZ-Ut4WCTXTGoUEP5MTWYjmZE$

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry@21:1/5 to All on Mon Nov 7 08:24:59 2022
    

    On 7 Nov 2022, at 03:15, Mike Dewhirst <miked@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Cameron Simpson on Mon Nov 7 09:05:53 2022
    Cameron Simpson <cs@cskk.id.au> wrote:
    On 06Nov2022 20:51, jak <nospam@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
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Chris Green on Mon Nov 7 09:28:18 2022
    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as '#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    Wouldn't '#! env python3' be better?

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Weatherby,Gerard@21:1/5 to rbowman on Mon Nov 7 11:56:19 2022
    Write the wrapper script.

    #!/bin/bash
    if [ $(hostname) == "oldystem" ]; then
    exec /usr/bin/python $*
    else
    exec /usr/bin/python2 $*
    fi

    From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Chris Green <cl@isbd.net>
    Date: Sunday, November 6, 2022 at 3:22 PM
    To: python-list@python.org <python-list@python.org>
    Subject: Re: How to manage python shebang on mixed systems?
    *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

    rbowman <bowman@montana.com> wrote:
    On Sun, 6 Nov 2022 10:03:50 +0000, Chris Green wrote:


    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 symlink?

    Not really, since the system where there's no python3 is one where I
    only have user access.

    --
    Chris Green
    ·
    -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gN4FcCAwYqK8bafmF2dr_EB9nahZcdNmU91bHuymdlakU27cNTnIwQ_FckF9PBZlllGnW_vlEjQ$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!
    Cn_UX_p3!gN4FcCAwYqK8bafmF2dr_EB9nahZcdNmU91bHuymdlakU27cNTnIwQ_FckF9PBZlllGnW_vlEjQ$>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From 2QdxY4RzWzUUiLuE@potatochowder.com@21:1/5 to Gerard" on Mon Nov 7 07:44:21 2022
    On 2022-11-07 at 11:56:19 +0000,
    "Weatherby,Gerard" <gweatherby@uchc.edu> wrote:

    Write the wrapper script.

    #!/bin/bash
    if [ $(hostname) == "oldystem" ]; then
    exec /usr/bin/python $*
    else
    exec /usr/bin/python2 $*
    fi

    Use "$@" (with the quotes) instead of a bare $*. The former preserves
    quoted arguments, while the latter does not.

    Also, it's probably more robust to check explcitly for python2 instead
    of depending on the hostname. Renaming hosts isn't a common operation,
    but in general, checking for what something *does* or *has* is better
    than checking for what it *is* (hey, look: duck typing!).

    Implementing both suggestions yields this:

    #! /bin/bash
    if [ -x /usr/bin/python2 ]; then
    exec /usr/bin/python2 "$@"
    else
    exec /usr/bin/python "$@"
    fi

    YMMV.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schachner, Joseph (US)@21:1/5 to All on Mon Nov 7 18:07:54 2022
    TWF5YmUgeW91IGNhbid0IGRvIHRoaXMsIGJ1dCBJIHdvdWxkIHN1Z2dlc3Qgb25seSBydW5uaW5n IG9uIHRoZSBQeXRob24gMyBzeXN0ZW1zLiAgUmVmdXNlIHRvIGp1bXAgdGhyb3VnaCBob29wcyBm b3IgdGhlIFB5dGhvbiAyIHN5c3RlbS4gIEl0IGlzIHllYXJzIG91dCBvZiBkYXRlLg0KSXQgaXMg bm90IGhhcmQgdG8gdXBncmFkZSBmcm9tIFB5dGhvbiAyIHRvIFB5dGhvbiAzLiAgVGhlcmUgaXMg YSAydG8zIHV0aWxpdHksIGFuZCBhZnRlciB0aGF0IHRoZXJlIHNob3VsZCBiZSB2ZXJ5IGZldyB0 aGluZ3MgeW91IHdhbnQgdG8gbWFudWFsbHkgY2hhbmdlLiAgUGVyaGFwcyB5b3UgY291bGQgZW5j b3VyYWdlIHRoZW0gdG8gdXBncmFkZS4NCg0KLS0tIEpvc2VwaCBTLg0KDQoNClRlbGVkeW5lIENv bmZpZGVudGlhbDsgQ29tbWVyY2lhbGx5IFNlbnNpdGl2ZSBCdXNpbmVzcyBEYXRhDQoNCi0tLS0t T3JpZ2luYWwgTWVzc2FnZS0tLS0tDQpGcm9tOiBDaHJpcy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3JpdGUgYSB0aW55IHNjcmlwdCBfbmFtZWRfICJweXRob24zIiB3 aGljaCBpbnZva2VzIHB5dGhvbiAyLiBJIA0KPiBrZWVwIGEgcGVyc29uYWwgIn4vYmluLWxvY2Fs IiBkaXJlY3RvcnkgZm9yIGp1c3Qgc3VjaCBwZXItc3lzdGVtIA0KPiBzcGVjaWFsIGNvbW1hbmRz IGxpa2UgdGhpcy4NCj4gMzogd2l0aCB5b3VyIHBzZXVkbyAicHl0aG9uMyIgc2NyaXB0IGluIHBs YWNlLCBtYWtlIGFsbCB0aGUgc2NyaXB0cyANCj4gdXNlIHRoZSAiIyEvdXNyL2Jpbi9lbnYgcHl0 aG9uMyIgc2hlYmFuZyBzdWdnZXN0ZWQgYWJvdmUuDQo+IA0KWWVzLCB0aGF0IHNvdW5kcyBhIGdv b2QgcGxhbiB0byBtZSwgdGhhbmtzIENhbWVyb24uDQoNCi0tDQpDaHJpcyBHcmVlbg0KwrcNCg==

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to All on Mon Nov 7 13:34:49 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@isbd.net>
    Sent: Monday, November 7, 2022 4:06 AM
    To: python-list@python.org
    Subject: Re: How to manage python shebang on mixed systems?

    Cameron Simpson <cs@cskk.id.au> wrote:
    On 06Nov2022 20:51, jak <nospam@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
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Thomas Passin on Tue Nov 8 06:29:14 2022
    On Tue, 8 Nov 2022 at 06:12, Thomas Passin <list1@tompassin.net> wrote:

    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.


    If currently-supported versions of any Linux distributions don't at
    least make a python3 command available, I would be very surprised. But
    when you don't have root access, it's not easy to install, even from repositories.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Joseph.Schachner@teledyne.com on Mon Nov 7 19:41:50 2022
    Schachner, Joseph (US) <Joseph.Schachner@teledyne.com> 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.

    Exactly! That's why I (OP) have only one system where this is a
    problem. The system is my hosting provider's cPanel platform which is
    running a very old Linux and, as I've said has only python 2.

    I can ask for python 3 on their system but I suspect that my voice is
    a very tiny one and there are higher priority things to get done.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Gollwitzer@21:1/5 to All on Mon Nov 7 20:58:08 2022
    Am 07.11.22 um 10:28 schrieb Chris Green:
    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as '#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    Wouldn't '#! env python3' be better?

    Does this even work? I thought that, since the #! is resolved by the
    kernel, an absolute path is required, and that the whole purpose of the /usr/bin/env thing is to search the path (because it is universally
    installed in /usr/bin) and to possibly set other environment variables



    Christian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Scott@21:1/5 to All on Mon Nov 7 20:48:48 2022
    On 7 Nov 2022, at 09:28, Chris Green <cl@isbd.net> wrote:

    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as '#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    env is always available as /usr/bin/env - I think its spec'ed in posix that way.

    The only reason that things are in /bin are for systems that need a subset of programs to boot the system to point it can mount /usr. env is not going to be needed for that use case.


    Wouldn't '#! env python3' be better?

    Barry


    --
    Chris Green
    ·
    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Chris Green on Tue Nov 8 08:54:39 2022
    On Tue, 8 Nov 2022 at 08:44, Chris Green <cl@isbd.net> wrote:

    Barry Scott <barry@barrys-emacs.org> wrote:


    On 7 Nov 2022, at 09:28, Chris Green <cl@isbd.net> wrote:

    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as '#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    env is always available as /usr/bin/env - I think its spec'ed in posix that way.

    The only reason that things are in /bin are for systems that need a subset of
    programs to boot the system to point it can mount /usr. env is not going to be
    needed for that use case.

    Given that the problem system is running a very old Linux I'm not sure
    what chance there is that it's fully posix compliant.

    If using "#!/usr/bin/env python3" is a way of avoiding problems if
    python3 isn't in /usr/bin then why is it any better depending on env
    being in /usr/bin.

    I frequently have python3 in /usr/local/bin instead. Sometimes in
    other places, depending on how many of them I've installed and from
    where. Using /usr/bin/env python3 ensures that it does the same as the "python3" command even if there's a venv active.

    (And yes, sometimes that's good, sometimes bad.)

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Barry Scott on Mon Nov 7 21:27:26 2022
    Barry Scott <barry@barrys-emacs.org> wrote:


    On 7 Nov 2022, at 09:28, Chris Green <cl@isbd.net> wrote:

    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as '#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    env is always available as /usr/bin/env - I think its spec'ed in posix that way.

    The only reason that things are in /bin are for systems that need a subset of programs to boot the system to point it can mount /usr. env is not going to be
    needed for that use case.

    Given that the problem system is running a very old Linux I'm not sure
    what chance there is that it's fully posix compliant.

    If using "#!/usr/bin/env python3" is a way of avoiding problems if
    python3 isn't in /usr/bin then why is it any better depending on env
    being in /usr/bin.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Cameron Simpson on Mon Nov 7 18:09:33 2022
    More discussion:

    https://unix.stackexchange.com/questions/29608/why-is-it-better-to-use-usr-bin-env-name-instead-of-path-to-name-as-my

    On 11/7/2022 5:35 PM, Cameron Simpson wrote:
    On 07Nov2022 09:28, Chris Green <cl@isbd.net> wrote:
    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as
    '#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    Wouldn't '#! env python3' be better?

    The thing after the shebang needs to be a full path.

    "env" is in /usr/bin on damn near everything. I think I had to make a
    symlink on a Solaris system once, but otherwise it has not been a
    problem for me on many different systems for many years.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Chris Green on Tue Nov 8 09:35:07 2022
    On 07Nov2022 09:28, Chris Green <cl@isbd.net> wrote:
    Chris Green <cl@isbd.net> wrote:
    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.

    Doesn't '#!/usr/bin/env python3' suffer from the same problem as >'#!/usr/bin/python3' in the sense that the env executable might not be
    in /usr/bin?

    Wouldn't '#! env python3' be better?

    The thing after the shebang needs to be a full path.

    "env" is in /usr/bin on damn near everything. I think I had to make a
    symlink on a Solaris system once, but otherwise it has not been a
    problem for me on many different systems for many years.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jak@21:1/5 to All on Tue Nov 8 09:01:31 2022
    Il 07/11/2022 20:41, Chris Green ha scritto:
    Schachner, Joseph (US) <Joseph.Schachner@teledyne.com> 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.

    Exactly! That's why I (OP) have only one system where this is a
    problem. The system is my hosting provider's cPanel platform which is
    running a very old Linux and, as I've said has only python 2.

    I can ask for python 3 on their system but I suspect that my voice is
    a very tiny one and there are higher priority things to get done.


    Perhaps this is because you speak to them about the problems of
    compatibility, try to speak to them about power, speed and performance, instead. ;-D

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Chris Green on Wed Nov 9 15:52:51 2022
    On 2022-11-07 21:27:26 +0000, Chris Green wrote:
    Barry Scott <barry@barrys-emacs.org> wrote:
    env is always available as /usr/bin/env - I think its spec'ed in posix that way.

    The only reason that things are in /bin are for systems that need a subset of
    programs to boot the system to point it can mount /usr. env is not going to be
    needed for that use case.

    Given that the problem system is running a very old Linux I'm not sure
    what chance there is that it's fully posix compliant.

    It doesn't have to be fully posix compliant. Just reasonably posix
    compliant.


    If using "#!/usr/bin/env python3" is a way of avoiding problems if
    python3 isn't in /usr/bin then why is it any better depending on env
    being in /usr/bin.

    Because env is a standard unix utility which has been in the same place
    for 30 years or so and is unlikely to be somewhere else or missing
    completely. Python3 OTOH is not a standard unix utility. It may not be
    there at all or it may be installed in /usr/local or /opt or even in the
    user's home directory.

    (Yes, of course "standard unix utilities" may be missing, too. For
    example on an embedded system there might only be the bare minimum to
    run the application. I even had a redhat system once which didn't have
    grep installed.)

    (Personally I avoid using env: I don't want my scripts to depend on the
    PATH. But that's a different issue.)

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmNrvr0ACgkQ8g5IURL+ KF0edw//UQw9Ps9CchJvipSKXQSJ8Lco/EWfMcU/qR0q156Vbq3IJ2wYAQMUYWEK LsahvSwtUWF+Wwoh46OCLWfBhPylxHHe0DilCeYqsNXzM9ifS8dOiMVWDFy3bFGK Ky59KL+J4nVwxks2Inag0s8xo6IHMJtYyeQMkIGJnhNlpE348QK8VVKCL7Gvg4sc IRX4PCDLWTu8F6tuwoK8k3V26xuMXvKULAok/+xoN6EBrLbyx0WbBMhbSDTxpom2 i75A2iKxDYC597i7+tB9GfT9GUo+HQW9SVm6jjjR2A3esCU1SVqX7+AsTJTX/xqw eqFNegLuTPqwvCTO1O/MBGGTzh2wAA94ybwouEX73ipHB8SB284x8MavKdULeHjO +0NMO7c9HaLOBZlmDlZ6r7AcaP6BSeEu37v0HioQ2u7HbOVv6iR9resZxMxiCJ/b VYJdYv3Vy1AbHCMKdbX+7UKcFVDzLk4Irv3YLyERUWalmZcscQ9T6D0QtEBHzrxB wiQpomzOaY5JcSO9kUUlMQyJ8Wmi8gcJM+QC0tiPvmKWjuauusrcElQuX4+GA17A mflZEKtm+1Kz4V/y+7LP33oSVARrYQpyquV0fYzcDZIC1WhLKnbKDl28xbbVS9Xe wxa8OWIhKNzN0XylIscX0xmRWefeaehzIBcYYdf