Skip to content

py.exe shebang processing only runs python version installed by pymanager #91

Closed

Description

@clint-lawrence

Describe the bug
When running a script with a shebang, only python versions installed by pymanager will be run. When the shebang specifies a python version installed with a different method, py.exe incorrectly uses the default interpreter version instead. The intended version is available from an existing installation and is shown in pymanage list

To Reproduce
On the system below Python 3.10 has been installed using pymanage, and Python3.12 is an existing installation.

PS C:\Users\clawrence\dev\pymanager> pymanager list
Tag               Name                  Managed By  Version   Alias
3.10[-64]         Python 3.10.11        PythonCore  3.10.11   python[w]3.10.exe, python[w]3.exe
3.14-dev[-64]  *  Python 3.14.0b1       PythonCore  3.14.0b1  python[w]3-64.exe, python[w]3.14-64.exe, python...

* These runtimes were found, but cannot be updated or uninstalled. *
3.13              Python 3.13 (64-bit)  PythonCore  3.13.0
3.13              Python 3.13 (64-bit)  PythonCore  3.13.3
3.12              Python 3.12 (64-bit)  PythonCore  3.12.7
3.11              Python 3.11 (64-bit)  PythonCore  3.11.9
3.10              Python 3.10 (64-bit)  PythonCore  3.10.11
3.8               Python 3.8 (64-bit)   PythonCore  3.8.10

Some columns were truncated. Use '--format=json' or '--format=jsonl' for full information.

Create two test scripts with a shebang that call the desired python version:

PS C:\Users\clawrence\dev\pymanager> "#! /usr/bin/env python3.12
>> import sys
>> print(sys.version)
>> print(sys.executable)" | Out-File -FilePath 3.12.py
PS C:\Users\clawrence\dev\pymanager> "#! /usr/bin/env python3.10
>> import sys
>> print(sys.version)
>> print(sys.executable)" | Out-File -FilePath 3.10.py

Calling Python 3.12 (which was not installed by pymanager) runs the default interpreter:

PS C:\Users\clawrence\dev\pymanager> py .\3.12.py
3.14.0b1 (tags/v3.14.0b1:b092705, May  7 2025, 10:22:31) [MSC v.1943 64 bit (AMD64)]
C:\Users\clawrence\AppData\Local\Python\pythoncore-3.14-64\python.exe

Calling Python 3.10 (installed by pymanager) runs correctly:

PS C:\Users\clawrence\dev\pymanager> py .\3.10.py
3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]

py.exe can run both interpreters correctly when specified as a cli arg:

C:\Users\clawrence\AppData\Local\Python\pythoncore-3.10-64\python.exe
PS C:\Users\clawrence\dev\pymanager> py -3.12 --version
Python 3.12.7
PS C:\Users\clawrence\dev\pymanager> py -3.10 --version
Python 3.10.11

Installing 3.12 using pymanager and the shebang works correctly:

PS C:\Users\clawrence\dev\pymanager> pymanager install 3.12
Installing Python 3.12.10.
Extracting: ...................................................................✅

Global shortcuts directory is not on PATH. Add it for easy access to global Python commands.
Directory to add: C:\Users\clawrence\AppData\Local\Python\bin

Python 3.12.10 will be launched by python3-64.exe, python3.12-64.exe, python3.12.exe, python3.exe, pythonw3-64.exe, pythonw3.12-64.exe, pythonw3.12.exe, pythonw3.exe
PS C:\Users\clawrence\dev\pymanager> py .\3.12.py
3.12.10 (tags/v3.12.10:0cc8128, Apr  8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)]
C:\Users\clawrence\AppData\Local\Python\pythoncore-3.12-64\python.exe
PS C:\Users\clawrence\dev\pymanager> pymanager uninstall 3.12
Uninstall Python 3.12.10? [Y/n] y
Removed Python 3.12.10
PS C:\Users\clawrence\dev\pymanager> py .\3.12.py
3.14.0b1 (tags/v3.14.0b1:b092705, May  7 2025, 10:22:31) [MSC v.1943 64 bit (AMD64)]
C:\Users\clawrence\AppData\Local\Python\pythoncore-3.14-64\python.exe
PS C:\Users\clawrence\dev\pymanager>

Expected behavior
py.exe should use the python version specified in the shebang, or return an error it the requested version isn't found.

Additional context

Activity

  1. zooba commented on May 12, 2025

    @zooba
    Member

    This is currently because we search by alias, and unmanaged installs don't have aliases defined.

    We need another fallback search that actually parses the command name and does another search of the installed runtimes. It should restrict to PythonCore, because tags/aliases are actually entirely arbitrary and are not "really" versions, so they can't be treated like that unless we limit the search space to ones we control. But it's probably good enough to detect python(3\.[\d.-abc]+) and convert to PythonCore\$1 for the search.

  2. self-assigned this
    on May 12, 2025
  3. added a commit that references this issue on May 12, 2025
    dda57a5
  4. added
    bugSomething isn't working
    and removed
    help wantedExtra attention is needed
    on May 12, 2025
  5. added a commit that references this issue on May 12, 2025
    9a150b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions