"py" command for Linux and Mac?

Michael Torrie torriem at gmail.com
Fri May 20 21:21:13 EDT 2022


On 5/12/22 11:59, De ongekruisigde wrote:
> On 2022-05-12, Mats Wichmann <mats at wichmann.us> wrote:
>> On 5/12/22 10:25, Dan Stromberg wrote:
>>> Hi folks.
>>>
>>> I heard there's a Windows-like "py" command for Linux (and Mac?).
>>>
>>> I'm finally getting to porting a particular project's Python 2.7 code to
>>> 3.x, and one of the first steps will probably be changing a lot of "python2
>>> script.py" to use #!/usr/bin/env python2 and chmod +x.  Then we can update
>>> the scripts one at a time to use #!/usr/bin/env python3.
>>>
>>> However, would this be Linux-and-Mac-only?  I'm not at all sure this code
>>> will ever move to Windows, but in case it does, would a "py" command work
>>> on all 3 if I use #!/usr/bin/env py?
>>
>> The py command (python lanucher) respects shebang lines.
> 
> Linux by itself respects shebang lines, so you don't need a separate
> launcher program. Just put e.g.:

Dan knows this already. His question is about whether the shebang should
instead refer to a py launcher so that this script will run on Windows
or Linux.

And of course the answer given by the grandparent is that Dan should use
a normal linux shebang line in his scripts and on Windows the py
launcher will read that shebang and guestimate the proper python
interpreter to use and execute the script with that. Thus if I'm reading
this correctly, a Linux shebang line should function as expected on
Windows when python files are associated and launched with the py.exe
launcher, even though there's no such thing as /usr/bin/python3 on
Windows.  Py launcher makes it work as if there was.




More information about the Python-list mailing list