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

Thomas Passin list1 at tompassin.net
Mon Jan 1 09:11:38 EST 2024


On 1/1/2024 8:19 AM, Thomas Passin via Python-list wrote:
> On 1/1/2024 6:02 AM, Sibylle Koczian via Python-list wrote:
>> Am 30.12.2023 um 04:04 schrieb Mike Dewhirst via Python-list:
>>>
>>> I had assumed the OP had installed Python from the Microsoft shop and 
>>> that's where py.exe must have come from.
>>>
>>
>> In fact I didn't say in my post that I always get Python from 
>> python.org. When I started to use the language there was no Python 
>> from any Microsoft shop (I'm not sure there was a Microsoft shop, it 
>> was in the last millenium, Python 1.5 or 1.6). So I tend to forget 
>> that possible download source.
>>
>> But in all this thread I didn't see a single explanation for my 
>> current situation: one and the same shebang line works on Windows 10 / 
>> Python 3.11 and doesn't work on Windows 11 / Python 3.12. I suspect 
>> Windows, because a change in the way Python 3.12 uses shebang lines 
>> should be visible in the documentation.
>>
>> Happy new year to all!
>> Sibylle
> 
> Happy New Year!
> 
> I speculated that the shebang line didn't work on Windows 10 either, but 
> you didn't realize it because the file associations were right to launch 
> ".py" programs with the right version of Python.  When the newer version 
> of Python got installed, the default Python program to use, was not 
> updated correctly, and the shebang line still has nothing to do with the 
> launch failure.  This could happen if other the older install went into 
> Program Files, while the newer one went into 
> %USERPROFILE%\AppData\Local\Programs\Python.
> 
> This was backed up with all of 5 minutes of experimenting on my own 
> computer, on which Windows launches ".py" programs with an old install 
> of Python 3.9.9, but the py launcher launches Python 3.12 by default.
> 
> Since I am avoiding Windows 11, I can't try anything on it, so my 
> thoughts above may not be relevant.
> 
> The Python docs for 3.12.1 cover shebang lines at
> 
> https://docs.python.org/3/using/windows.html
> 
> "If the first line of a script file starts with #!, it is known as a 
> “shebang” line. Linux and other Unix like operating systems have native 
> support for such lines and they are commonly used on such systems to 
> indicate how a script should be executed. This launcher allows the same 
> facilities to be used with Python scripts on Windows and the examples 
> above demonstrate their use.
> 
> 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
> 
> For example, if the first line of your script starts with
> 
> #! /usr/bin/python
> The default Python will be located and used. As many Python scripts 
> written to work on Unix will already have this line, you should find 
> these scripts can be used by the launcher without modification. If you 
> are writing a new script on Windows which you hope will be useful on 
> Unix, you should use one of the shebang lines starting with /usr."
> 
> But
> 
> "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. The environment variable PYLAUNCHER_NO_SEARCH_PATH may 
> be set (to any value) to skip this search of PATH.
> 
> Shebang lines that do not match any of these patterns are looked up in 
> the [commands] section of the launcher’s .INI file. This may be used to 
> handle certain commands in a way that makes sense for your system. The 
> name of the command must be a single argument (no spaces in the shebang 
> executable), and the value substituted is the full path to the 
> executable (additional arguments specified in the .INI will be quoted as 
> part of the filename)."
> 

Here's how to find out what program Windows thinks it should use to run 
a ".py" file.  In a console:

C:\Users\tom>assoc .py
.py=Python.File

C:\Users\tom>ftype Python.file
Python.file="C:\Windows\py.exe" "%L" %*

If your ".py" files are associated to the py.exe launcher, as mine are, 
then the launcher may try to use your shebang line and you need to make 
sure there aren't any spaces where there shouldn't be.

If your ".py" files are not associated with py.exe, the shebang line 
probably won't be used for anything.




More information about the Python-list mailing list