Support transparent Python patch version upgrades (#13954)
> NOTE: The PRs that were merged into this feature branch have all been independently reviewed. But it's also useful to see all of the changes in their final form. I've added comments to significant changes throughout the PR to aid discussion. This PR introduces transparent Python version upgrades to uv, allowing for a smoother experience when upgrading to new patch versions. Previously, upgrading Python patch versions required manual updates to each virtual environment. Now, virtual environments can transparently upgrade to newer patch versions. Due to significant changes in how uv installs and executes managed Python executables, this functionality is initially available behind a `--preview` flag. Once an installation has been made upgradeable through `--preview`, subsequent operations (like `uv venv -p 3.10` or patch upgrades) will work without requiring the flag again. This is accomplished by checking for the existence of a minor version symlink directory (or junction on Windows). ### Features * New `uv python upgrade` command to upgrade installed Python versions to the latest available patch release: ``` # Upgrade specific minor version uv python upgrade 3.12 --preview # Upgrade all installed minor versions uv python upgrade --preview ``` * Transparent upgrades also occur when installing newer patch versions: ``` uv python install 3.10.8 --preview # Automatically upgrades existing 3.10 environments uv python install 3.10.18 ``` * Support for transparently upgradeable Python `bin` installations via `--preview` flag ``` uv python install 3.13 --preview # Automatically upgrades the `bin` installation if there is a newer patch version available uv python upgrade 3.13 --preview ``` * Virtual environments can still be tied to a patch version if desired (ignoring patch upgrades): ``` uv venv -p 3.10.8 ``` ### Implementation Transparent upgrades are implemented using: * Minor version symlink directories (Unix) or junctions (Windows) * On Windows, trampolines simulate paths with junctions * Symlink directory naming follows Python build standalone format: e.g., `cpython-3.10-macos-aarch64-none` * Upgrades are scoped to the minor version key (as represented in the naming format: implementation-minor version+variant-os-arch-libc) * If the context does not provide a patch version request and the interpreter is from a managed CPython installation, the `Interpreter` used by `uv python run` will use the full symlink directory executable path when available, enabling transparently upgradeable environments created with the `venv` module (`uv run python -m venv`) New types: * `PythonMinorVersionLink`: in a sense, the core type for this PR, this is a representation of a minor version symlink directory (or junction on Windows) that points to the highest installed managed CPython patch version for a minor version key. * `PythonInstallationMinorVersionKey`: provides a view into a `PythonInstallationKey` that excludes the patch and prerelease. This is used for grouping installations by minor version key (e.g., to find the highest available patch installation for that minor version key) and for minor version directory naming. ### Compatibility * Supports virtual environments created with: * `uv venv` * `uv run python -m venv` (using managed Python that was installed or upgraded with `--preview`) * Virtual environments created within these environments * Existing virtual environments from before these changes continue to work but aren't transparently upgradeable without being recreated * Supports both standard Python (`python3.10`) and freethreaded Python (`python3.10t`) * Support for transparently upgrades is currently only available for managed CPython installations Closes #7287 Closes #7325 Closes #7892 Closes #9031 Closes #12977 --------- Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
@@ -26,6 +26,7 @@ use uv_platform_tags::{Tags, TagsError};
|
||||
use uv_pypi_types::{ResolverMarkerEnvironment, Scheme};
|
||||
|
||||
use crate::implementation::LenientImplementationName;
|
||||
use crate::managed::ManagedPythonInstallations;
|
||||
use crate::platform::{Arch, Libc, Os};
|
||||
use crate::pointer_size::PointerSize;
|
||||
use crate::{
|
||||
@@ -168,7 +169,7 @@ impl Interpreter {
|
||||
Ok(path) => path,
|
||||
Err(err) => {
|
||||
warn!("Failed to find base Python executable: {err}");
|
||||
uv_fs::canonicalize_executable(base_executable)?
|
||||
canonicalize_executable(base_executable)?
|
||||
}
|
||||
};
|
||||
Ok(base_python)
|
||||
@@ -263,6 +264,21 @@ impl Interpreter {
|
||||
self.prefix.is_some()
|
||||
}
|
||||
|
||||
/// Returns `true` if this interpreter is managed by uv.
|
||||
///
|
||||
/// Returns `false` if we cannot determine the path of the uv managed Python interpreters.
|
||||
pub fn is_managed(&self) -> bool {
|
||||
let Ok(installations) = ManagedPythonInstallations::from_settings(None) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
installations
|
||||
.find_all()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|install| install.path() == self.sys_base_prefix)
|
||||
}
|
||||
|
||||
/// Returns `Some` if the environment is externally managed, optionally including an error
|
||||
/// message from the `EXTERNALLY-MANAGED` file.
|
||||
///
|
||||
@@ -483,10 +499,19 @@ impl Interpreter {
|
||||
/// `python-build-standalone`.
|
||||
///
|
||||
/// See: <https://github.com/astral-sh/python-build-standalone/issues/382>
|
||||
#[cfg(unix)]
|
||||
pub fn is_standalone(&self) -> bool {
|
||||
self.standalone
|
||||
}
|
||||
|
||||
/// Returns `true` if an [`Interpreter`] may be a `python-build-standalone` interpreter.
|
||||
// TODO(john): Replace this approach with patching sysconfig on Windows to
|
||||
// set `PYTHON_BUILD_STANDALONE=1`.`
|
||||
#[cfg(windows)]
|
||||
pub fn is_standalone(&self) -> bool {
|
||||
self.standalone || (self.is_managed() && self.markers().implementation_name() == "cpython")
|
||||
}
|
||||
|
||||
/// Return the [`Layout`] environment used to install wheels into this interpreter.
|
||||
pub fn layout(&self) -> Layout {
|
||||
Layout {
|
||||
@@ -608,6 +633,29 @@ impl Interpreter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls `fs_err::canonicalize` on Unix. On Windows, avoids attempting to resolve symlinks
|
||||
/// but will resolve junctions if they are part of a trampoline target.
|
||||
pub fn canonicalize_executable(path: impl AsRef<Path>) -> std::io::Result<PathBuf> {
|
||||
let path = path.as_ref();
|
||||
debug_assert!(
|
||||
path.is_absolute(),
|
||||
"path must be absolute: {}",
|
||||
path.display()
|
||||
);
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if let Ok(Some(launcher)) = uv_trampoline_builder::Launcher::try_from_path(path) {
|
||||
Ok(dunce::canonicalize(launcher.python_path)?)
|
||||
} else {
|
||||
Ok(path.to_path_buf())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fs_err::canonicalize(path)
|
||||
}
|
||||
|
||||
/// The `EXTERNALLY-MANAGED` file in a Python installation.
|
||||
///
|
||||
/// See: <https://packaging.python.org/en/latest/specifications/externally-managed-environments/>
|
||||
@@ -935,7 +983,7 @@ impl InterpreterInfo {
|
||||
|
||||
// We check the timestamp of the canonicalized executable to check if an underlying
|
||||
// interpreter has been modified.
|
||||
let modified = uv_fs::canonicalize_executable(&absolute)
|
||||
let modified = canonicalize_executable(&absolute)
|
||||
.and_then(Timestamp::from_path)
|
||||
.map_err(|err| {
|
||||
if err.kind() == io::ErrorKind::NotFound {
|
||||
|
||||
Reference in New Issue
Block a user