Skip to content

py ./x.py no longer works on Windows #98650

Closed
@CAD97

Description

@CAD97

Workaround for Windows developers

Put the following as py.ini in %LOCALAPPDATA%:

[commands]
bash=python

This will make py interpret the #!/usr/bin/env bash shebang as running python.

You can also put py.ini alongside py.exe. This is likely C:\Windows; you can find it with where pycmd or Get-Command pypwsh.

Alternatively, run x.py with py -3 to explicitly select to use the default Python 3 version rather than looking at the shebang. You should also be able to set ftype Python.File="C:\Windows\py.exe" "-3" "%L" %* so ./x.py will use py -3 to run.

To debug, set $env:PYLAUNCH_DEBUG=1pwsh/set PYLAUNCH_DEBUG=1cmd.


Issue description

#98474 added a #!/usr/bin/env bash shebang line to x.py to allow ./x.py to work on unixy platforms with python3 but not python (the previous shebang line was #!/usr/bin/env python).

This means that x.py no longer works on Windows machines which run python via the py Python Launcher for Windows, as py tries to interpret the shebang line (in order to use the correct python version) and ends up instead trying to run /usr/bin/env, which obviously doesn't exist on a Windows machine. Explicitly asking for a specific version from py (e.g. by running py -3 for the latest 3.x version) will successfully run the script, as then py ignores the shebang line.

In theory this is an upstream bug; py should not be trying to run something that isn't python. In practice, though, this is how most Windows users will be launching python, as this is the launcher set up to handle .py files when installing python from the official installer from the website or Python.Python.3 in winget. (The msstore python which the default-installed python and python3 shims point to reportedly do not install the py launcher.) Additionally, this worked fine previously, and only broke with #98474.

#71818 (comment) reports that #!/usr/bin/env python didn't work on Windows; I believe this is due to the order of events used in the described testing. @Walther first installed the msstore python. They then uninstalled the msstore python and installed the official python distribution, with "add python 3.10 to PATH" unchecked, reporting it to be unchecked by default. However, it was my experience that the official distribution did add python to the path by default; I suspect that the reason it didn't work for Walther was because (parts of) the msstore python was still in PATH, and the installer did not want to overwrite an existing installation. With my installation, a python and python3 shebang both work, and a python2 shebang results in a "Requested Python version (2) is not installed".

Changing the shebang to python3 has previously been delayed due to concerns about Windows compatibility. The specific situation seems to be this:

  • When python is handled through the py.exe launcher (i.e. the shell uses the ftype registered handler), all python virtual commands will seamlessly work on Windows.
  • However, when using a MSYS-like shell (e.g. Git Bash) which directly interprets shebangs, it will try to run the python3 program, which most likely does not exist on a Windows machine. (E.g. in my winget install of Python.Python.3, I have python.exe and python3.dll in path, but no python3.exe.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-bootstrapRelevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions