Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clint-lawrence opened this issue May 12, 2025 · 1 comment · Fixed by #97
Closed

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

clint-lawrence opened this issue May 12, 2025 · 1 comment · Fixed by #97
Assignees
Labels
bug Something isn't working

Comments

@clint-lawrence
Copy link

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

@zooba
Copy link
Member

zooba commented May 12, 2025

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.

@zooba zooba added the help wanted Extra attention is needed label May 12, 2025
@zooba zooba self-assigned this May 12, 2025
zooba added a commit to zooba/pymanager-python that referenced this issue May 12, 2025
@zooba zooba added bug Something isn't working and removed help wanted Extra attention is needed labels May 12, 2025
@zooba zooba closed this as completed in #97 May 12, 2025
@zooba zooba closed this as completed in 9a150b3 May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants