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 22:56:45 EST 2023


On 12/22/2023 7:27 PM, Michael Torrie via Python-list wrote:
> 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.

I actually don't remember how to set up the association for Python 
files.  I just always type the "py" launcher anyway, as in

py -m pip instal ...

I think that the association with py.exe must only happen if you install 
to Program Files.  As I said in my previous post, Windows still sticks 
with launching Python files with Python 3.9 even though I'm three 
version beyond that.  3.9 is the only one I installed to Program Files.

In my experience one should always make sure to know what version of 
Python is being used, at least if there is more than one version 
installed on the computer.  Even on Linux using a shebang line can be 
tricky, because you are likely to get the system's version of Python, 
and that often is not what you want.  OTOH you don't want to go 
symlinking python3 to some other version of python because then the OS 
system may not work right.  So either you have to specify the Python 
version in the shebang, or just specify the right version on the command 
line.  In that case you might as well not have included the shebang line 
at all.

I may be more sensitive to this issue because I run many different Linux 
distros in VMs to check a few programs I support to make sure they can 
run on Linux as well as Windows.  What Linux, you ask?  Well, who knows 
what our users will use? So I'm always getting Python version 
mix-and-match problems.  The system will still be at Python 3.10 but I 
need Python 3.11.  The system uses Python 3.11 but we shouldn't (or 
cannot) install our dependencies so we need a parallel install.  Etc, etc.

It's just better not to make assumptions about which version of Python 
will be running. Just specify it yourself when you can, and then you can 
be sure.


More information about the Python-list mailing list