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

Michael Torrie torriem at gmail.com
Fri Dec 22 19:27:58 EST 2023


On 12/22/23 07:02, Thomas Passin via Python-list wrote:
> On my Windows 10 machine, Python scripts run without a shebang line. 
> Perhaps Windows 11 has added the ability to use one, but then you would 
> need to use the actual location of your Python executable.

Yes if you associate .py or .pyw with python.exe (or pythonw.exe), then
things work as you describe.  However it's no longer recommended to do
that.

Instead---and I think this is the default now when you install
python---you should associate both .py and .pyw files with the py
launcher (py.exe) and it will examine the shebang line of the script and
determine which version of python to run. As I said this should work
regardless of the path listed in the shebang.  Note that the shebang is
meaningless to Windows itself, and Windows Explorer. It is only
meaningful to the py launcher.  So it's customary to just use a
unix-style shebang in your python scripts.  So either #!/usr/bin/python3
or #!/usr/bin/env python3 as you would in unix.

Using the py launcher as your Windows association with .py and.pyw files
you can have multiple versions of python installed and everything works
as it should, according to your shebang, just like on Unix.


More information about the Python-list mailing list