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

Thomas Passin list1 at tompassin.net
Fri Dec 22 13:42:05 EST 2023


On 12/22/2023 9:29 AM, Sibylle Koczian via Python-list wrote:
> Am 22.12.2023 um 14:13 schrieb Barry:
>>
>>
>>> On 22 Dec 2023, at 12:39, Sibylle Koczian via Python-list 
>>> <python-list at python.org> wrote:
>>>
>>> Hello,
>>>
>>> I always install Python on Windows in the same manner:
>>>
>>> - Python is not on the path,
>>> - it is installed for all users,
>>> - the Python Launcher is installed for all users,
>>> - the file types .py, .pyw etc. are associated with Python.
>>>
>>> My shebang line is usually "#!/usr/bin/env python3".
>>>
>>> This has always worked well. I could run Python scripts in a console
>>> window entering just the script name, by double clicking in the explorer
>>> or using WIN+r; the two last variants for GUI or for scripts with
>>> something like "input('Leave with Enter')" at the end.
>>>
>>> Now I've got a new computer with Windows 11 and I've installed Python
>>> 3.12.1. On my older machine it's Windows 10 and Python 3.11.5. Reading
>>> the Python documentation it seems my shebang lines should work as before
>>> - but they don't. The error message:
>>>
>>> "Unable to create process using 'C:\usr\bin\env\python
>>> "C:\Eigen\Src\launcher_versuche.py" ': Das System kann die angegebene
>>> Datei nicht finden."
>>>
>>> Without the "env" in the shebang line and only without it everything
>>> works as expected - but that's contrary to the documentation, isn't it?
>>
>> This suggests a typo in the shebang line. Is there a space between env 
>> and python?
>>
>> Barry
>>
> 
> Tried several variants with the same script:
> 
> #!/usr/bin/env python3
> # That's how I wrote it for Windows 10 / Python 3.11. It works there.
> 
> #!/usr/bin/env python
> #!/usr/bin/env/python
> 
> The error messages vary a little. This is a German Windows installation,
> the two variants with the space produce the same German error message,
> the third produces the message I've put into my first description.
> 
> The working variant on Windows 11 / Python 3.12 is "#!/usr/bin python".

There is some important context that is missing here.  Python on Windows 
does not normally install to that location.  That is not even a Windows 
path, neither by directory name nor by path separators.

In addition, Powershell and cmd.exe do not use a shebang line, at least 
through Windows 10.  Instead, they use whatever executable has been 
registered for a file extension.  This may or may not be the version you 
think.  On my system, the OS will use Python 3.9, but actually the most 
recent Python version on my system is Python 3.12. I can demonstrate the 
difference:  here is a tiny Python file with a shebang line, called 
showme.py:

#! %USERPROFILE%\AppData\Local\Programs\Python\Python312\python.exe
import sys
print(sys.executable)

Run this with the "py" launcher:
py showme.py
# prints C:\Users\tom\AppData\Local\Programs\Python\Python312\python.exe

Run it by invoking just the script's name:
showme.py
# prints C:\Program Files\Python39\python.exe

In neither case is the shebang line used.

This makes me think that maybe the Linux subsystem for Windows is being 
used here. If so, possibly the syntax for a shebang line has been 
tightened up, or there's a typo.  Either way, I would not automatically 
assume that Windows (at least through Windows 10) ever used the shebang 
line for launching these scripts.





More information about the Python-list mailing list