Python 3.12.1, Windows 11: shebang line #!/usr/bin/env python3 doesn't work any more

Thomas Passin list1 at tompassin.net
Sun Jan 14 09:53:58 EST 2024


On 1/14/2024 8:54 AM, Thomas Passin via Python-list wrote:
> On 1/14/2024 7:48 AM, Sibylle Koczian via Python-list wrote:
>> Am 09.01.2024 um 12:36 schrieb Barry Scott via Python-list:
>>>
>>>
>>>> On 7 Jan 2024, at 15:09, Sibylle Koczian via Python-list 
>>>> <python-list at python.org> wrote:
>>>>
>>>> Oh, and the two Windows and Python versions are on two different 
>>>> computers.
>>>>
>>>> Will remove the "/env" from my shebang lines, even if I don't 
>>>> understand what's happening.
>>>
>>> Thanks for the details.
>>>
>>> Only thing I can think of is that "python" may be defaulting to mean 
>>> python 2.
>>> If you use "#!/usr/bin/env python3" it may work on both.
>>
>> No, it doesn't. That's the form I started with. When it didn't work I 
>> thought "python3" might be too old, because Python 2 is dead for so long.
>>>
>>> Did you creates a py.ini file to configure py.exe?
>>>
>>> See if you have %userappdata%\py.ini on either windows 10 or windows 11.
>>> If so what is its contents?
>>
>> No to both.
>>>
>>> I've tried with and without a py.ini and cannot duplicate what you see.
>>>
>>
>> It really seems strange. Only thing I can think of - and I don't 
>> really believe in that idea: as far as I know in Windows 11 the 
>> handling of PATH has changed. My Python isn't on the path, perhaps 
>> that is it. A shebang line without "/env" doesn't check the path, right?
> 
>  From what I've read recently, if you have a Python program that starts 
> with a shebang line with any of four standard unix-like paths, then 
> Python (not Windows) will look for a version of Python in standard 
> locations - *NOT* in the shebang line locations:

I meant to write "the Python launcher", that is, the "py" program. 
Normal Python installs on Windows install the launcher and Windows will 
run it on ".py" files if no other program has been specified on the 
command line.

> "To allow shebang lines in Python scripts to be portable between Unix 
> and Windows, this launcher supports a number of ‘virtual’ commands to 
> specify which interpreter to use. The supported virtual commands are:
> 
> /usr/bin/env
> /usr/bin/python
> /usr/local/bin/python
> python
> "
> 
> Also -
> "The /usr/bin/env form of shebang line has one further special property. 
> Before looking for installed Python interpreters, this form will search 
> the executable PATH for a Python executable matching the name provided 
> as the first argument. This corresponds to the behaviour of the Unix env 
> program, which performs a PATH search. If an executable matching the 
> first argument after the env command cannot be found, but the argument 
> starts with python, it will be handled as described for the other 
> virtual commands.
> "
> 
> There are some other complications, too, depending on whether you 
> specify bare "python" or some specific version. The form with 
> "/usr/bin/env" is the closest to the unix behavior, in that it searches 
> the PATH.  And you write that your intended version of Python is not on 
> the path.
> 
> IOW, these shebang lines don't work the way you seem to think that they do.
> 
> See https://docs.python.org/3/using/windows.html for a more complete 
> rundown.



More information about the Python-list mailing list