[Python-Dev] PEP 379 Python launcher for Windows - behaviour for #!/usr/bin/env python line is wrong

Paul Moore p.f.moore at gmail.com
Fri May 3 22:23:07 CEST 2013

While reviewing the behaviour of Vinay's "distil" installer tool (see
distutils-sig for details, but it's not relevant here) I have found what I
think is a flaw in the behaviour of the py.exe launcher for Windows.

To recap for people unfamiliar with the launcher, it emulates #! line
interpretation on Windows, interpreting commonly used forms from Unix and
launching the appropriate installed Python interpreter (finding its
location from the registry, as python.exe may not be on PATH).

The problem is with the interpretation of #!/usr/bin/env python. The
launcher treats this the same as #!/usr/bin/python, launching the "default"
Python. But that is *not* what the equivalent line does on Unix, where it
launches the *currently active* Python (a crucial difference when there is
an active virtualenv). The result is that a script written to run with the
active Python works on Unix as expected, but can use an unexpected version
of Python on Windows. This is particularly unpleasant when the program in
question is an (un)installer like distil!

I would propose that the behaviour of the launcher on Windows should be
changed when it encounters specifically the hashbang line #!/usr/bin/env
python. In that case, it should search PATH for a copy of python.exe, and
if it finds one, use that. If there is no python.exe on PATH, it should
fall back to the same version of Python as would have been used if the line
were #!/usr/bin/python.

This will mean that scripts written with #!/usr/bin/env python will behave
the same on Unix and Windows in the presence of activated virtualenvs.

Would people be happy with this change? If so I will open an issue on
bugs.python.org. I can look at producing a patch, as well.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130503/681e700b/attachment.html>

More information about the Python-Dev mailing list