Skip to content
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

Use py launcher to select Python interpreter #1002

Merged
merged 20 commits into from
Jun 27, 2023

Conversation

danyeaw
Copy link
Sponsor Contributor

@danyeaw danyeaw commented Jun 18, 2023

  • I have added an entry to docs/changelog.md

Summary of changes

This PR closes #940 by adding support to use the py launcher to select the Python interpreter when the --python option is provided.

The feature is used like pipx install --python 3.10 cowsay to install using the Python 3.10 interpreter. The only way to do this right now is by passing in the full path to the interpreter.

I also did some refactoring in nearby code to leave the codebase in better shape than I found it.

Test plan

Tested by running

> pipx install --python 3.10 cowsay`
> pipx reinstall --python 3.11 cowsay`
> pipx uninstall cowsay`
> pipx run --python 3.9 cowsay hi`

pipx install --python 3.100 cowsay raises a FileNotFound error, which is similar to the behavior in Linux if you run pipx install --python python3.100 cowsay.

src/pipx/interpreter.py Outdated Show resolved Hide resolved
@uranusjr
Copy link
Member

Some thoughts:

  1. There’s an unofficial py launcher that works cross platform. While it is not a part of the official distribution, it may be a good idea to extend this feature to all platforms since it costs us nothing.
  2. I wonder if we should unify the value format a bit. For example, if py is present in PATH and the user provides pythonX.Y, maybe we could automatically strip python and try py -X.Y? Not sure.
  3. On the flip side, if the user gives X.Y and py is not found, maybe we can automatically try pythonX.Y in Python? I am less in favour of this one; seems a bit too magical.

@danyeaw
Copy link
Sponsor Contributor Author

danyeaw commented Jun 20, 2023

Hi @uranusjr, thanks so much for the feedback! I haven't used py launcher in Linux before, but I can investigate adding support for it as well. Could that be part of a follow-up PR?

For the 2nd and 3rd points, I thought that your direction in the original issue was to avoid magic.

The nice thing about the 2nd option would be that a single command could be used across platforms, but no magic is fine as well.

@uranusjr
Copy link
Member

The second point is mainly to maintain existing usages. We already support --python python.X.Y and it would be nice to make it automatically work on Windows.

The UNIX launcher can be found here: https://github.com/brettcannon/python-launcher
It’s mostly compatible with the Windows one, so maybe the best way to approach this is to simply drop the Windows check in this PR and investigate further later.

src/pipx/commands/run.py Outdated Show resolved Hide resolved
src/pipx/main.py Outdated Show resolved Hide resolved
src/pipx/main.py Outdated Show resolved Hide resolved
@danyeaw
Copy link
Sponsor Contributor Author

danyeaw commented Jun 24, 2023

Hi team, is there anything else needed before we merge this?

src/pipx/commands/run.py Outdated Show resolved Hide resolved
src/pipx/main.py Outdated Show resolved Hide resolved
@danyeaw danyeaw requested a review from dukecat0 June 24, 2023 15:23
@danyeaw danyeaw changed the title Windows: use py launcher to select Python interpreter Use py launcher to select Python interpreter Jun 25, 2023
src/pipx/main.py Outdated Show resolved Hide resolved
src/pipx/main.py Show resolved Hide resolved
src/pipx/main.py Show resolved Hide resolved
src/pipx/main.py Show resolved Hide resolved
@dukecat0 dukecat0 merged commit 91b11b2 into pypa:main Jun 27, 2023
@dukecat0
Copy link
Member

Thanks!

@danyeaw danyeaw deleted the py-launcher-version branch June 27, 2023 23:41
@danyeaw
Copy link
Sponsor Contributor Author

danyeaw commented Jun 27, 2023

Thanks team for your reviews and getting this merged, I think this will be a nice improvement to pipx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Windows: Use the Python Launcher to Select Python Version
4 participants