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

fix "env use python" to use Python in PATH #10187

Merged
merged 1 commit into from
Feb 16, 2025

Conversation

radoering
Copy link
Member

@radoering radoering commented Feb 16, 2025

This should use the Python, which is in the PATH, not the latest Python.

Pull Request Check List

Resolves: #10186

  • Added tests for changed code.
  • Updated documentation for changed code.

Summary by Sourcery

Fix the env use python command to select the Python executable specified in the user's PATH.

Bug Fixes:

  • Use the Python executable found in the user's PATH when running env use python instead of the latest Python version available.

Tests:

  • Add tests to verify that the correct Python executable is selected when using env use python.

Copy link

sourcery-ai bot commented Feb 16, 2025

Reviewer's Guide by Sourcery

This pull request modifies the way Poetry locates Python executables. It now uses shutil.which to search for Python in the system's PATH when the env use python command is executed. This ensures that the Python executable in the PATH is used, rather than assuming the latest installed version. The pull request also includes new tests to verify the functionality of the changes.

Sequence diagram for locating Python executable

sequenceDiagram
  participant User
  participant Poetry
  participant ShutilWhichPythonProvider
  participant findpython

  User->>Poetry: env use python
  Poetry->>ShutilWhichPythonProvider: find_python_by_name("python")
  ShutilWhichPythonProvider->>shutil: which("python")
  alt Python found in PATH
    shutil-->>ShutilWhichPythonProvider: Path to python
    ShutilWhichPythonProvider->>findpython: PythonVersion(executable=Path(path))
    findpython-->>ShutilWhichPythonProvider: PythonVersion object
    ShutilWhichPythonProvider-->>Poetry: PythonVersion object
    Poetry->>Poetry: Use PythonVersion object to create Python environment
  else Python not found in PATH
    shutil-->>ShutilWhichPythonProvider: None
    ShutilWhichPythonProvider-->>Poetry: None
    Poetry->>findpython: find(python_name)
  end
Loading

File-Level Changes

Change Details Files
Modified Python.get_by_name to use shutil.which to find Python executables in the system's PATH.
  • Replaced direct path existence checks and from_executable calls with ShutilWhichPythonProvider.find_python_by_name to locate Python.
  • Removed the from_executable method from the Python class.
src/poetry/utils/env/python/manager.py
Implemented ShutilWhichPythonProvider to find Python executables using shutil.which.
  • Created ShutilWhichPythonProvider with find_pythons and find_python_by_name methods.
  • The find_pythons method now returns a list containing the Python executable found using shutil.which('python').
  • The find_python_by_name method uses shutil.which to find a Python executable by a given name.
src/poetry/utils/env/python/providers.py
tests/utils/env/python/test_python_providers.py
Added tests for Python.get_by_name and ShutilWhichPythonProvider.
  • Added tests to verify that Python.get_by_name correctly identifies Python versions based on name.
  • Added tests to verify that ShutilWhichPythonProvider correctly finds Python executables in the PATH.
tests/utils/env/python/test_manager.py
tests/utils/env/python/test_python_providers.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @radoering - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a test case where python_name in get_by_name is an absolute path to a python executable.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@radoering radoering requested a review from a team February 16, 2025 13:47
# if it is a path try assuming it is an executable
return cls.from_executable(python_name)
if python := ShutilWhichPythonProvider.find_python_by_name(python_name):
return cls(python=python)

if python := findpython.find(python_name):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker for this, but I am definitely curious if the reason why findpython.find(python_name) does not work as expected here has to do with how the action sets up the Python installation.

This should use the Python, which is in the PATH, not the latest Python.
@radoering radoering merged commit 1791aa4 into python-poetry:main Feb 16, 2025
53 checks passed
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.

Poetry 2.1.0 regression: poetry env use python uses the max python not the path python
2 participants