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

Mats Wichmann mats at wichmann.us
Mon Jan 15 13:26:16 EST 2024


On 1/15/24 09:44, Sibylle Koczian via Python-list wrote:

> First and foremost I want to understand why I'm seeing this:
> 
> - Python scripts with "/usr/bin/env python3" as shebang line work as 
> expected on a computer with Windows 10 and Python 3.11.5. They have 
> worked for years on this machine, using either the latest Python or one 
> version before (depending on availability of some packages). There is a 
> virtual machine with ArchLinux on the same machine and some of the 
> scripts are copies from that.
> 
> - I've got a second computer with Windows 11 and I installed Python 
> 3.12.1 on it. After copying some scripts from my first computer I found 
> that I couldn't start them: not by entering the script name in a 
> console, not using py.exe, not double clicking in the explorer. Entering 
> <full_path_to_python.exe>\python <scriptname> probably worked - I think 
> I tried that too, but I'm not really sure, because that's really not 
> practical.
> 
> In the Python documentation for versions 3.11 and 3.12 I found no 
> differences regarding py.exe and shebang lines.
> 
> Then I removed the "/env" from the shebang lines and could start the 
> scripts from the second computer. That certainly is a solution, but why???

It's because of Windows itself.  The default nowadays is that irritating 
little stub that prompts you to go install Python from the WIndows 
store.  When you use the "env" form, it looks for python (or python3 in 
your case) in the PATH *first* and you'll get a hit.   Mine looks like:

C:\Users\mats\AppData\Local\Microsoft\WindwsApps\python.exe and python3.exe

you can check what it's doing for you by using the "where" command in a 
windows shell.

On your older Windows 10 machine you either never had that stub - I 
don't know when it was added, maybe someone from Microsoft listening 
here knows - or it's been superseded by changes to the PATH, or 
something.  On my fairly new Win 11 box the base of that path is early 
in the user portion of PATH, so that must be a default.

py.exe without the "/usr/bin/env" magic doesn't put PATH searching 
first, according to that snip from the docs that's been posted here 
several times., so you shouldn't fall down that particular rathole.



More information about the Python-list mailing list