From 877d37c3203290fe39c52f4b69254bfd3a7f49d5 Mon Sep 17 00:00:00 2001 From: Erick Guan <297343+erickguan@users.noreply.github.com> Date: Sat, 5 Oct 2024 09:40:42 +0200 Subject: [PATCH] feat: use uv to create python's venv by default --- docs/lang/python.md | 11 ++++++++ e2e/plugins/core/test_python_uv_venv | 39 ++++++++++++++++++++++++++++ schema/mise.json | 4 +++ settings.toml | 5 ++++ src/cli/settings/ls.rs | 2 ++ src/cli/settings/set.rs | 1 + src/cli/settings/unset.rs | 1 + src/config/env_directive.rs | 31 ++++++++++++++-------- 8 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 e2e/plugins/core/test_python_uv_venv diff --git a/docs/lang/python.md b/docs/lang/python.md index ff4f91c592..7d6e0da863 100644 --- a/docs/lang/python.md +++ b/docs/lang/python.md @@ -101,6 +101,17 @@ Packages list to install with pip after installing a Python version. Automatically create a virtualenv in the directory specified by `_.python.venv` if it doesn't exist. +Deprecated note: Use `env._python.venv` instead. + +### `python_venv_stdlib` + +* Type: `bool` +* Env: `MISE_PYTHON_VENV_STDLIB` +* Default: `false` + +Prefer to use venv from Python's standard library. +By default, mise will prioritize `uv` for the virtual environment if `uv` is in the `PATH`. + ## Default Python packages mise can automatically install a default set of Python packages with pip right after installing a diff --git a/e2e/plugins/core/test_python_uv_venv b/e2e/plugins/core/test_python_uv_venv new file mode 100644 index 0000000000..d765113ee5 --- /dev/null +++ b/e2e/plugins/core/test_python_uv_venv @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +export MISE_PYTHON_DEFAULT_PACKAGES_FILE="$HOME/.default-python-packages" + +cat >.mise.toml <subdir/.mise.toml <, input: Vec<(EnvDirective, PathBuf)>, ) -> eyre::Result { - let settings = Settings::get(); let mut ctx = BASE_CONTEXT.clone(); trace!("resolve: input: {:#?}", &input); let mut env = initial @@ -224,7 +224,7 @@ impl EnvResults { } } EnvDirective::Source(input) => { - settings.ensure_experimental("env._.source")?; + SETTINGS.ensure_experimental("env._.source")?; trust_check(&source)?; let s = r.parse_template(&ctx, &source, input.to_string_lossy().as_ref())?; for p in xx::file::glob(normalize_path(&config_root, s.into()))? { @@ -258,13 +258,6 @@ impl EnvResults { .into_iter() .chain(env::split_paths(&env_vars[&*PATH_KEY])) .collect::>(); - let cmd = CmdLineRunner::new("python3") - .args(["-m", "venv", &venv.to_string_lossy()]) - .envs(&env_vars) - .env( - PATH_KEY.to_string(), - env::join_paths(&path)?.to_string_lossy().to_string(), - ); if ts .list_missing_versions() .iter() @@ -272,6 +265,22 @@ impl EnvResults { { debug!("python not installed, skipping venv creation"); } else { + let cmd = if let (false, Some(_uv_in_path)) = + (SETTINGS.python_venv_stdlib, which_non_pristine("uv")) + { + CmdLineRunner::new("uv").args(["venv", &venv.to_string_lossy()]) + } else { + CmdLineRunner::new("python3").args([ + "-m", + "venv", + &venv.to_string_lossy(), + ]) + } + .envs(&env_vars) + .env( + PATH_KEY.to_string(), + env::join_paths(&path)?.to_string_lossy().to_string(), + ); info!("creating venv at: {}", display_path(&venv)); cmd.execute()?; }