Add an environment variable for UV_VENV_RELOCATABLE (#18331)

Closes https://github.com/astral-sh/uv/issues/18319.
This commit is contained in:
Charlie Marsh
2026-03-05 18:40:39 -05:00
committed by GitHub
parent 9f53c5866f
commit eec0c284d0
5 changed files with 33 additions and 1 deletions
+2 -1
View File
@@ -3166,7 +3166,7 @@ pub struct VenvArgs {
#[arg(long)]
pub system_site_packages: bool,
/// Make the virtual environment relocatable.
/// Make the virtual environment relocatable [env: UV_VENV_RELOCATABLE=]
///
/// A relocatable virtual environment can be moved around and redistributed without invalidating
/// its associated entrypoint and activation scripts.
@@ -3179,6 +3179,7 @@ pub struct VenvArgs {
/// absolute paths), the entrypoints and scripts themselves will _not_ be relocatable. In other
/// words, copying those entrypoints and scripts to a location outside the environment will not
/// work, as they reference paths relative to the environment itself.
#[expect(clippy::doc_markdown)]
#[arg(long, overrides_with("no_relocatable"))]
pub relocatable: bool,
+2
View File
@@ -663,6 +663,7 @@ pub struct EnvironmentOptions {
pub no_env_file: EnvFlag,
pub venv_seed: EnvFlag,
pub venv_clear: EnvFlag,
pub venv_relocatable: EnvFlag,
pub init_bare: EnvFlag,
}
@@ -756,6 +757,7 @@ impl EnvironmentOptions {
no_env_file: EnvFlag::new(EnvVars::UV_NO_ENV_FILE)?,
venv_seed: EnvFlag::new(EnvVars::UV_VENV_SEED)?,
venv_clear: EnvFlag::new(EnvVars::UV_VENV_CLEAR)?,
venv_relocatable: EnvFlag::new(EnvVars::UV_VENV_RELOCATABLE)?,
init_bare: EnvFlag::new(EnvVars::UV_INIT_BARE)?,
})
}
+5
View File
@@ -477,6 +477,11 @@ impl EnvVars {
#[attr_added_in("0.8.0")]
pub const UV_VENV_CLEAR: &'static str = "UV_VENV_CLEAR";
/// Equivalent to the `--relocatable` command-line argument. If set, the virtual
/// environment will be relocatable.
#[attr_added_in("0.10.8")]
pub const UV_VENV_RELOCATABLE: &'static str = "UV_VENV_RELOCATABLE";
/// Install seed packages (one or more of: `pip`, `setuptools`, and `wheel`) into the virtual environment
/// created by `uv venv`.
///
+1
View File
@@ -3489,6 +3489,7 @@ impl VenvSettings {
// Resolve flags from CLI and environment variables.
let seed = seed || environment.venv_seed.value == Some(true);
let clear = clear || environment.venv_clear.value == Some(true);
let relocatable = relocatable || environment.venv_relocatable.value == Some(true);
Self {
seed,
+23
View File
@@ -1307,6 +1307,29 @@ fn verify_pyvenv_cfg_relocatable() {
activate_csh.assert(predicates::path::missing());
}
/// With `UV_VENV_RELOCATABLE=1`, the virtual environment is relocatable.
#[test]
fn verify_pyvenv_cfg_relocatable_env_var() {
let context = uv_test::test_context!("3.12");
// Create a virtual environment at `.venv` with the env var.
context
.venv()
.arg(context.venv.as_os_str())
.arg("--clear")
.arg("--python")
.arg("3.12")
.env("UV_VENV_RELOCATABLE", "1")
.assert()
.success();
let pyvenv_cfg = context.venv.child("pyvenv.cfg");
pyvenv_cfg.assert(predicates::path::is_file());
// Relocatable flag is set.
pyvenv_cfg.assert(predicates::str::contains("relocatable = true"));
}
/// With `relocatable-envs-default` preview feature, venvs are relocatable by default.
#[test]
fn relocatable_envs_default_preview() {