-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add support for --prefix and --with installations in find_uv_bin
#14184
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,16 +16,29 @@ def find_uv_bin() -> str: | |
| targets = [ | ||
| # The scripts directory for the current Python | ||
| sysconfig.get_path("scripts"), | ||
| # The scripts directory for the base prefix (if different) | ||
| # The scripts directory for the base prefix | ||
| sysconfig.get_path("scripts", vars={"base": sys.base_prefix}), | ||
| # The user scheme scripts directory, e.g., `~/.local/bin` | ||
| sysconfig.get_path("scripts", scheme=_user_scheme()), | ||
| # Adjacent to the package root, e.g. from, `pip install --target` | ||
| os.path.join(os.path.dirname(os.path.dirname(__file__)), "bin"), | ||
| # Above the package root, e.g., from `pip install --prefix` or `uv run --with` | ||
| ( | ||
| # On Windows, with module path `<prefix>/Lib/site-packages/uv` | ||
| _join(_matching_parents(_module_path(), "Lib/site-packages/uv"), "Scripts") | ||
| if sys.platform == "win32" | ||
| # On Unix, with module path `<prefix>/lib/python3.13/site-packages/uv` | ||
| else _join( | ||
| _matching_parents(_module_path(), "lib/python*/site-packages/uv"), "bin" | ||
| ) | ||
| ), | ||
| # Adjacent to the package root, e.g., from `pip install --target` | ||
| # with module path `<target>/uv` | ||
| _join(_matching_parents(_module_path(), "uv"), "bin"), | ||
| ] | ||
|
|
||
| seen = [] | ||
| for target in targets: | ||
| if not target: | ||
| continue | ||
| if target in seen: | ||
| continue | ||
| seen.append(target) | ||
|
|
@@ -39,6 +52,45 @@ def find_uv_bin() -> str: | |
| ) | ||
|
|
||
|
|
||
| def _module_path() -> str | None: | ||
| path = os.path.dirname(__file__) | ||
| return path | ||
|
|
||
|
|
||
| def _matching_parents(path: str | None, match: str) -> str | None: | ||
| """ | ||
| Return the parent directory of `path` after trimming a `match` from the end. | ||
| The match is expected to contain `/` as a path separator, while the `path` | ||
| is expected to use the platform's path separator (e.g., `os.sep`). The path | ||
| components are compared case-insensitively and a `*` wildcard can be used | ||
| in the `match`. | ||
| """ | ||
| from fnmatch import fnmatch | ||
|
|
||
| if not path: | ||
| return None | ||
| parts = path.split(os.sep) | ||
| match_parts = match.split("/") | ||
| if len(parts) < len(match_parts): | ||
| return None | ||
|
|
||
| if not all( | ||
| fnmatch(part, match_part) | ||
| for part, match_part in zip( | ||
| reversed(parts), reversed(match_parts), strict=False | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't support Python 3.8 and 3.9. |
||
| ) | ||
| ): | ||
| return None | ||
|
|
||
| return os.sep.join(parts[: -len(match_parts)]) | ||
|
|
||
|
|
||
| def _join(path: str | None, *parts: str) -> str | None: | ||
| if not path: | ||
| return None | ||
| return os.path.join(path, *parts) | ||
|
|
||
|
|
||
| def _user_scheme() -> str: | ||
| if sys.version_info >= (3, 10): | ||
| user_scheme = sysconfig.get_preferred_scheme("user") | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do this last? This could easily pick up a different, global uv installation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually happens for me:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's what we're discussing at #14184 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consider this a distinct change, will be addressed by #14191