Skip to main content

uv_python/
virtualenv.rs

1use std::borrow::Cow;
2use std::str::FromStr;
3use std::{
4    env, io,
5    path::{Path, PathBuf},
6};
7
8use fs_err as fs;
9use thiserror::Error;
10
11use uv_preview::PreviewFeature;
12use uv_pypi_types::Scheme;
13use uv_static::EnvVars;
14
15use crate::PythonVersion;
16
17/// The layout of a virtual environment.
18#[derive(Debug)]
19pub struct VirtualEnvironment {
20    /// The absolute path to the root of the virtualenv, e.g., `/path/to/.venv`.
21    pub root: PathBuf,
22
23    /// The path to the Python interpreter inside the virtualenv, e.g., `.venv/bin/python`
24    /// (Unix, Python 3.11).
25    pub executable: PathBuf,
26
27    /// The path to the base executable for the environment, within the `home` directory.
28    pub base_executable: PathBuf,
29
30    /// The [`Scheme`] paths for the virtualenv, as returned by (e.g.) `sysconfig.get_paths()`.
31    pub scheme: Scheme,
32}
33
34/// A parsed `pyvenv.cfg`
35#[derive(Debug, Clone)]
36pub struct PyVenvConfiguration {
37    /// The `PYTHONHOME` directory containing the base Python executable.
38    pub(crate) home: Option<PathBuf>,
39    /// Was the virtual environment created with the `virtualenv` package?
40    pub(crate) virtualenv: bool,
41    /// Was the virtual environment created with the `uv` package?
42    pub(crate) uv: bool,
43    /// Is the virtual environment relocatable?
44    pub(crate) relocatable: bool,
45    /// Was the virtual environment populated with seed packages?
46    pub(crate) seed: bool,
47    /// Should the virtual environment include system site packages?
48    pub(crate) include_system_site_packages: bool,
49    /// The Python version the virtual environment was created with
50    pub(crate) version: Option<PythonVersion>,
51}
52
53#[derive(Debug, Error)]
54pub enum Error {
55    #[error(transparent)]
56    Io(#[from] io::Error),
57    #[error("Broken virtual environment `{0}`: `pyvenv.cfg` is missing")]
58    MissingPyVenvCfg(PathBuf),
59    #[error("Broken virtual environment `{0}`: `pyvenv.cfg` could not be parsed")]
60    ParsePyVenvCfg(PathBuf, #[source] io::Error),
61}
62
63/// Locate an active virtual environment by inspecting environment variables.
64///
65/// Supports `VIRTUAL_ENV`.
66pub(crate) fn virtualenv_from_env() -> Option<PathBuf> {
67    if let Some(dir) = env::var_os(EnvVars::VIRTUAL_ENV).filter(|value| !value.is_empty()) {
68        return Some(PathBuf::from(dir));
69    }
70
71    None
72}
73
74#[derive(Debug, PartialEq, Eq, Copy, Clone)]
75pub(crate) enum CondaEnvironmentKind {
76    /// The base Conda environment; treated like a system Python environment.
77    Base,
78    /// Any other Conda environment; treated like a virtual environment.
79    Child,
80}
81
82impl CondaEnvironmentKind {
83    /// Whether the given `CONDA_PREFIX` path is the base Conda environment.
84    ///
85    /// The base environment is typically stored in a location matching the `_CONDA_ROOT` path.
86    ///
87    /// Additionally, when the base environment is active, `CONDA_DEFAULT_ENV` will be set to a
88    /// name, e.g., `base`, which does not match the `CONDA_PREFIX`, e.g., `/usr/local` instead of
89    /// `/usr/local/conda/envs/<name>`. Note the name `CONDA_DEFAULT_ENV` is misleading, it's the
90    /// active environment name, not a constant base environment name.
91    fn from_prefix_path(path: &Path) -> Self {
92        // Pixi never creates true "base" envs and names project envs "default", confusing our
93        // heuristics, so treat Pixi prefixes as child envs outright.
94        if is_pixi_environment(path) {
95            return Self::Child;
96        }
97
98        // If `_CONDA_ROOT` is set and matches `CONDA_PREFIX`, it's the base environment.
99        if let Ok(conda_root) = env::var(EnvVars::CONDA_ROOT) {
100            if path == Path::new(&conda_root) {
101                return Self::Base;
102            }
103        }
104
105        // Next, we'll use a heuristic based on `CONDA_DEFAULT_ENV`
106        let Ok(current_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
107            return Self::Child;
108        };
109
110        // If the `CONDA_PREFIX` equals the `CONDA_DEFAULT_ENV`, we're in an unnamed environment
111        // which is typical for environments created with `conda create -p /path/to/env`.
112        if path == Path::new(&current_env) {
113            return Self::Child;
114        }
115
116        // If the environment name is "base" or "root", treat it as a base environment
117        //
118        // These are the expected names for the base environment; and is retained for backwards
119        // compatibility, but can be removed with the `special-conda-env-names` preview feature.
120        if !uv_preview::is_enabled(PreviewFeature::SpecialCondaEnvNames)
121            && (current_env == "base" || current_env == "root")
122        {
123            return Self::Base;
124        }
125
126        // For other environment names, use the path-based logic
127        let Some(name) = path.file_name() else {
128            return Self::Child;
129        };
130
131        // If the environment is in a directory matching the name of the environment, it's not
132        // usually a base environment.
133        if name.to_str().is_some_and(|name| name == current_env) {
134            Self::Child
135        } else {
136            Self::Base
137        }
138    }
139}
140
141/// Detect whether the current `CONDA_PREFIX` belongs to a Pixi-managed environment.
142fn is_pixi_environment(path: &Path) -> bool {
143    path.join("conda-meta").join("pixi").is_file()
144}
145
146/// Locate an active conda environment by inspecting environment variables.
147///
148/// If `base` is true, the active environment must be the base environment or `None` is returned,
149/// and vice-versa.
150pub(crate) fn conda_environment_from_env(kind: CondaEnvironmentKind) -> Option<PathBuf> {
151    let dir = env::var_os(EnvVars::CONDA_PREFIX).filter(|value| !value.is_empty())?;
152    let path = PathBuf::from(dir);
153
154    if kind != CondaEnvironmentKind::from_prefix_path(&path) {
155        return None;
156    }
157
158    Some(path)
159}
160
161/// Locate a virtual environment by searching the file system.
162///
163/// Searches for a `.venv` directory in the current or any parent directory. If the current
164/// directory is itself a virtual environment (or a subdirectory of a virtual environment), the
165/// containing virtual environment is returned.
166pub(crate) fn virtualenv_from_working_dir() -> Result<Option<PathBuf>, Error> {
167    let current_dir = crate::current_dir()?;
168
169    for dir in current_dir.ancestors() {
170        // If we're _within_ a virtualenv, return it.
171        if uv_fs::is_virtualenv_base(dir) {
172            return Ok(Some(dir.to_path_buf()));
173        }
174
175        // Otherwise, search for a `.venv` directory.
176        let dot_venv = dir.join(".venv");
177        if dot_venv.is_dir() {
178            if !uv_fs::is_virtualenv_base(&dot_venv) {
179                return Err(Error::MissingPyVenvCfg(dot_venv));
180            }
181            return Ok(Some(dot_venv));
182        }
183    }
184
185    Ok(None)
186}
187
188/// Returns the path to the `python` executable inside a virtual environment.
189pub(crate) fn virtualenv_python_executable(venv: impl AsRef<Path>) -> PathBuf {
190    let venv = venv.as_ref();
191    if cfg!(windows) {
192        // Search for `python.exe` in the `Scripts` directory.
193        let default_executable = venv.join("Scripts").join("python.exe");
194        if default_executable.exists() {
195            return default_executable;
196        }
197
198        // Apparently, Python installed via msys2 on Windows _might_ produce a POSIX-like layout.
199        // See: https://github.com/PyO3/maturin/issues/1108
200        let executable = venv.join("bin").join("python.exe");
201        if executable.exists() {
202            return executable;
203        }
204
205        // Fallback for Conda environments.
206        let executable = venv.join("python.exe");
207        if executable.exists() {
208            return executable;
209        }
210
211        // If none of these exist, return the standard location
212        default_executable
213    } else {
214        // Check for both `python3` over `python`, preferring the more specific one
215        let default_executable = venv.join("bin").join("python3");
216        if default_executable.exists() {
217            return default_executable;
218        }
219
220        let executable = venv.join("bin").join("python");
221        if executable.exists() {
222            return executable;
223        }
224
225        // If none of these exist, return the standard location
226        default_executable
227    }
228}
229
230impl PyVenvConfiguration {
231    /// Parse a `pyvenv.cfg` file into a [`PyVenvConfiguration`].
232    pub fn parse(cfg: impl AsRef<Path>) -> Result<Self, Error> {
233        let mut home = None;
234        let mut virtualenv = false;
235        let mut uv = false;
236        let mut relocatable = false;
237        let mut seed = false;
238        let mut include_system_site_packages = true;
239        let mut version = None;
240
241        // Per https://snarky.ca/how-virtual-environments-work/, the `pyvenv.cfg` file is not a
242        // valid INI file, and is instead expected to be parsed by partitioning each line on the
243        // first equals sign.
244        let content = fs::read_to_string(&cfg)
245            .map_err(|err| Error::ParsePyVenvCfg(cfg.as_ref().to_path_buf(), err))?;
246        for line in content.lines() {
247            let Some((key, value)) = line.split_once('=') else {
248                continue;
249            };
250            match key.trim() {
251                "home" => {
252                    home = Some(PathBuf::from(value.trim()));
253                }
254                "virtualenv" => {
255                    virtualenv = true;
256                }
257                "uv" => {
258                    uv = true;
259                }
260                "relocatable" => {
261                    relocatable = value.trim().to_lowercase() == "true";
262                }
263                "seed" => {
264                    seed = value.trim().to_lowercase() == "true";
265                }
266                "include-system-site-packages" => {
267                    include_system_site_packages = value.trim().to_lowercase() == "true";
268                }
269                "version" | "version_info" => {
270                    version = Some(
271                        PythonVersion::from_str(value.trim())
272                            .map_err(|e| io::Error::new(std::io::ErrorKind::InvalidData, e))?,
273                    );
274                }
275                _ => {}
276            }
277        }
278
279        Ok(Self {
280            home,
281            virtualenv,
282            uv,
283            relocatable,
284            seed,
285            include_system_site_packages,
286            version,
287        })
288    }
289
290    /// Returns true if the virtual environment was created with the `virtualenv` package.
291    pub fn is_virtualenv(&self) -> bool {
292        self.virtualenv
293    }
294
295    /// Returns true if the virtual environment was created with the uv package.
296    pub fn is_uv(&self) -> bool {
297        self.uv
298    }
299
300    /// Returns true if the virtual environment is relocatable.
301    pub(crate) fn is_relocatable(&self) -> bool {
302        self.relocatable
303    }
304
305    /// Returns true if the virtual environment was populated with seed packages.
306    pub fn is_seed(&self) -> bool {
307        self.seed
308    }
309
310    /// Returns true if the virtual environment should include system site packages.
311    pub fn include_system_site_packages(&self) -> bool {
312        self.include_system_site_packages
313    }
314
315    /// Set the key-value pair in the `pyvenv.cfg` file.
316    pub fn set(content: &str, key: &str, value: &str) -> String {
317        let mut lines = content.lines().map(Cow::Borrowed).collect::<Vec<_>>();
318        let mut found = false;
319        for line in &mut lines {
320            if let Some((lhs, _)) = line.split_once('=') {
321                if lhs.trim() == key {
322                    *line = Cow::Owned(format!("{key} = {value}"));
323                    found = true;
324                    break;
325                }
326            }
327        }
328        if !found {
329            lines.push(Cow::Owned(format!("{key} = {value}")));
330        }
331        if lines.is_empty() {
332            String::new()
333        } else {
334            format!("{}\n", lines.join("\n"))
335        }
336    }
337}
338
339#[cfg(test)]
340mod tests {
341    use std::ffi::OsStr;
342
343    use indoc::indoc;
344    use temp_env::with_vars;
345    use tempfile::tempdir;
346
347    use super::*;
348
349    #[test]
350    fn pixi_environment_is_treated_as_child() {
351        let tempdir = tempdir().unwrap();
352        let prefix = tempdir.path();
353        let conda_meta = prefix.join("conda-meta");
354
355        fs::create_dir_all(&conda_meta).unwrap();
356        fs::write(conda_meta.join("pixi"), []).unwrap();
357
358        let vars = [
359            (EnvVars::CONDA_ROOT, None),
360            (EnvVars::CONDA_PREFIX, Some(prefix.as_os_str())),
361            (EnvVars::CONDA_DEFAULT_ENV, Some(OsStr::new("example"))),
362        ];
363
364        with_vars(vars, || {
365            assert_eq!(
366                CondaEnvironmentKind::from_prefix_path(prefix),
367                CondaEnvironmentKind::Child
368            );
369        });
370    }
371
372    #[test]
373    fn test_set_existing_key() {
374        let content = indoc! {"
375            home = /path/to/python
376            version = 3.8.0
377            include-system-site-packages = false
378        "};
379        let result = PyVenvConfiguration::set(content, "version", "3.9.0");
380        assert_eq!(
381            result,
382            indoc! {"
383                home = /path/to/python
384                version = 3.9.0
385                include-system-site-packages = false
386            "}
387        );
388    }
389
390    #[test]
391    fn test_set_new_key() {
392        let content = indoc! {"
393            home = /path/to/python
394            version = 3.8.0
395        "};
396        let result = PyVenvConfiguration::set(content, "include-system-site-packages", "false");
397        assert_eq!(
398            result,
399            indoc! {"
400                home = /path/to/python
401                version = 3.8.0
402                include-system-site-packages = false
403            "}
404        );
405    }
406
407    #[test]
408    fn test_set_key_no_spaces() {
409        let content = indoc! {"
410            home=/path/to/python
411            version=3.8.0
412        "};
413        let result = PyVenvConfiguration::set(content, "include-system-site-packages", "false");
414        assert_eq!(
415            result,
416            indoc! {"
417                home=/path/to/python
418                version=3.8.0
419                include-system-site-packages = false
420            "}
421        );
422    }
423
424    #[test]
425    fn test_set_key_prefix() {
426        let content = indoc! {"
427            home = /path/to/python
428            home_dir = /other/path
429        "};
430        let result = PyVenvConfiguration::set(content, "home", "new/path");
431        assert_eq!(
432            result,
433            indoc! {"
434                home = new/path
435                home_dir = /other/path
436            "}
437        );
438    }
439
440    #[test]
441    fn test_set_empty_content() {
442        let content = "";
443        let result = PyVenvConfiguration::set(content, "version", "3.9.0");
444        assert_eq!(
445            result,
446            indoc! {"
447                version = 3.9.0
448            "}
449        );
450    }
451}