Skip to content

python3.exe ignores PYTHON_MANAGER_DEFAULT when the script has shebang #249

Closed

Description

@DarkDaskin

Install source and version

  • Installed from the Windows Store
  • Installed with the MSIX from python.org
  • Installed with the MSI from python.org
  • Installed with winget install 9NQ7512CXL7T

Version: 25.2.240.0

Describe the bug
I wanted to use the PYTHON_MANAGER_DEFAULT environment variable to select specific Python version to use in the current shell session. It works for invoking python3 directly or running scripts without shebang, but scripts with shebang always run in the latest available Python version, which may break them.

To Reproduce
Steps to reproduce the behavior:

  1. Launch cmd.exe
  2. Invoke py and let it install the default Python version (which is 3.14 currently)
  3. Invoke py install 3.8
  4. Invoke python3 --version and see that it selects Python 3.14
  5. Switch to Python 3.8 with set PYTHON_MANAGER_DEFAULT=3.8
  6. Invoke python3 --version and see that it selects Python 3.8
  7. Prepare a script test.py with the following content:
#!/usr/bin/env python3
import sys
print(sys.version)
  1. Invoke python3 test.py and see that it selects Python 3.14

Expected behavior
The script runs in Python 3.8.

Additional context
PEP 773 states that it should match a global alias listed for an install. Currently it works this way, but it is counter-intuitive and makes it not possible to choose Python version when a script relies on global python3 being of specific version. The PEP also states that the specific patterns to be detected are left to the implementation, so respecting PYTHON_MANAGER_DEFAULT in case of #!/usr/bin/env python3 does not seemingly contradict the spec.

Activity

  1. zooba commented on Jan 12, 2026

    @zooba
    Member

    It should match py -3, which is going to be the latest 3.x version available from PythonCore, so this behaviour is correct, if potentially unintuitive. It matches the intended behaviour for a python3 symlink on POSIX (which doesn't always work like that, because there are ways to break it, but it's the intended behaviour to provide the latest Python 3.x).

    The alternative behaviour - work like py - raises the question of what to do if the default tag is not a 3.x version? Should python3 launch Python 2.x if that's what is specified? Or should it choose arbitrarily? Or should it fail?

  2. DarkDaskin commented on Jan 13, 2026

    @DarkDaskin
    Author

    Just checked how it works with Python 2.x:

    >set PYTHON_MANAGER_DEFAULT=2.7
    >python3 --version
    Python 2.7.18
    

    So it seems that python3.exe works as an alias for python.exe and already will launch Python 2.x when that is the chosen version.
    This issue is not about python vs python3 command, it is about executing scripts with and without shebang. It is counterintuitive that presence of shebang alters already present version choice logic.

  3. zooba commented on Jan 13, 2026

    @zooba
    Member

    So it seems that python3.exe works as an alias for python.exe and already will launch Python 2.x when that is the chosen version.

    Oh okay then, my mistake.

    It is counterintuitive that presence of shebang alters already present version choice logic.

    It's also counterintuitive that it would launch the default when there's information available telling it which one it should use.

    The shebangs are actually pretty confusing in general, and I wish we didn't have to support them at all. But I guess in this case it's finding the python3 and interpreting it like py -V:3 (because that's what it'd do for e.g. python3.14 --> -V:3.14) and so it overrides the default.

    I'd need to look back at the spec to see how we described this case. The only thing we'd change is to treat python3 commands the same as python commands, assuming the specs allow it.

  4. multiplemonomials commented on Mar 31, 2026

    @multiplemonomials

    Just ran into this and it confused the fsck out of me. Would really appreciate if we could fix this so PYTHON_MANAGER_DEFAULT is respected for scripts with shebangs.

  5. zooba commented on Mar 31, 2026

    @zooba
    Member

    @multiplemonomials Just for clarity, you had a script with a /bin/python3-like shebang (specifically the python3 part) that you launched with py or python and expected it to choose PYTHON_MANAGER_DEFAULT?

    If the script had a different shebang, or you used a different command to launch it, then the change I've proposed here is not likely to help you and we'll have to look at other ways to reduce the confusion.

  6. multiplemonomials commented on Mar 31, 2026

    @multiplemonomials

    Yes that's exactly what happened to me. It seems incredibly unintuitive that running python3 on the command line does not use the same python version as a script that has python3 in the shebang.

  7. added 2 commits that reference this issue on Mar 31, 2026
    531f811
    9ea31a5
  8. added a commit that references this issue on Mar 31, 2026
    4f10964
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions