Skip to main content

uv_python/
environment.rs

1use std::borrow::Cow;
2use std::fmt;
3use std::path::{Path, PathBuf};
4use std::sync::Arc;
5
6use owo_colors::OwoColorize;
7use tracing::debug;
8
9use uv_cache::Cache;
10use uv_fs::{LockedFile, LockedFileError, Simplified};
11use uv_pep440::Version;
12
13use crate::discovery::find_python_installation;
14use crate::installation::PythonInstallation;
15use crate::virtualenv::{PyVenvConfiguration, virtualenv_python_executable};
16use crate::{
17    EnvironmentPreference, Error, Interpreter, Prefix, PythonNotFound, PythonPreference,
18    PythonRequest, Target,
19};
20
21/// A Python environment, consisting of a Python [`Interpreter`] and its associated paths.
22#[derive(Debug, Clone)]
23pub struct PythonEnvironment(Arc<PythonEnvironmentShared>);
24
25#[derive(Debug, Clone)]
26struct PythonEnvironmentShared {
27    root: PathBuf,
28    interpreter: Interpreter,
29}
30
31/// The result of failed environment discovery.
32///
33/// Generally this is cast from [`PythonNotFound`] by [`PythonEnvironment::find`].
34#[derive(Clone, Debug, Error)]
35pub struct EnvironmentNotFound {
36    request: PythonRequest,
37    preference: EnvironmentPreference,
38}
39
40#[derive(Clone, Debug, Error)]
41pub struct InvalidEnvironment {
42    path: PathBuf,
43    pub kind: InvalidEnvironmentKind,
44}
45#[derive(Debug, Clone)]
46pub enum InvalidEnvironmentKind {
47    NotDirectory,
48    Empty,
49    MissingExecutable(PathBuf),
50}
51
52impl From<PythonNotFound> for EnvironmentNotFound {
53    fn from(value: PythonNotFound) -> Self {
54        Self {
55            request: value.request,
56            preference: value.environment_preference,
57        }
58    }
59}
60
61impl fmt::Display for EnvironmentNotFound {
62    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63        #[derive(Debug, Copy, Clone)]
64        enum SearchType {
65            /// Only virtual environments were searched.
66            Virtual,
67            /// Only system installations were searched.
68            System,
69            /// Both virtual and system installations were searched.
70            VirtualOrSystem,
71        }
72
73        impl fmt::Display for SearchType {
74            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75                match self {
76                    Self::Virtual => write!(f, "virtual environment"),
77                    Self::System => write!(f, "system Python installation"),
78                    Self::VirtualOrSystem => {
79                        write!(f, "virtual environment or system Python installation")
80                    }
81                }
82            }
83        }
84
85        let search_type = match self.preference {
86            EnvironmentPreference::Any => SearchType::VirtualOrSystem,
87            EnvironmentPreference::ExplicitSystem => {
88                if self.request.is_explicit_system() {
89                    SearchType::VirtualOrSystem
90                } else {
91                    SearchType::Virtual
92                }
93            }
94            EnvironmentPreference::OnlySystem => SearchType::System,
95            EnvironmentPreference::OnlyVirtual => SearchType::Virtual,
96        };
97
98        if matches!(self.request, PythonRequest::Default | PythonRequest::Any) {
99            write!(f, "No {search_type} found")?;
100        } else {
101            write!(f, "No {search_type} found for {}", self.request)?;
102        }
103
104        match search_type {
105            // This error message assumes that the relevant API accepts the `--system` flag. This
106            // is true of the callsites today, since the project APIs never surface this error.
107            SearchType::Virtual => write!(
108                f,
109                "; run `{}` to create an environment, or pass `{}` to install into a non-virtual environment",
110                "uv venv".green(),
111                "--system".green()
112            )?,
113            SearchType::VirtualOrSystem => {
114                write!(f, "; run `{}` to create an environment", "uv venv".green())?;
115            }
116            SearchType::System => {}
117        }
118
119        Ok(())
120    }
121}
122
123impl fmt::Display for InvalidEnvironment {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        write!(
126            f,
127            "Invalid environment at `{}`: {}",
128            self.path.user_display(),
129            self.kind
130        )
131    }
132}
133
134impl fmt::Display for InvalidEnvironmentKind {
135    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136        match self {
137            Self::NotDirectory => write!(f, "expected directory but found a file"),
138            Self::MissingExecutable(path) => {
139                write!(f, "missing Python executable at `{}`", path.user_display())
140            }
141            Self::Empty => write!(f, "directory is empty"),
142        }
143    }
144}
145
146impl PythonEnvironment {
147    /// Find a [`PythonEnvironment`] matching the given request and preference.
148    ///
149    /// If looking for a Python interpreter to create a new environment, use [`PythonInstallation::find`]
150    /// instead.
151    pub fn find(
152        request: &PythonRequest,
153        preference: EnvironmentPreference,
154        python_preference: PythonPreference,
155        cache: &Cache,
156    ) -> Result<Self, Error> {
157        let installation =
158            match find_python_installation(request, preference, python_preference, cache)? {
159                Ok(installation) => installation,
160                Err(err) => return Err(EnvironmentNotFound::from(err).into()),
161            };
162        Ok(Self::from_installation(installation))
163    }
164
165    /// Create a [`PythonEnvironment`] from the virtual environment at the given root.
166    ///
167    /// N.B. This function also works for system Python environments and users depend on this.
168    pub fn from_root(root: impl AsRef<Path>, cache: &Cache) -> Result<Self, Error> {
169        debug!(
170            "Checking for Python environment at: `{}`",
171            root.as_ref().user_display()
172        );
173        match root.as_ref().try_exists() {
174            Ok(true) => {}
175            Ok(false) => {
176                return Err(Error::MissingEnvironment(EnvironmentNotFound {
177                    preference: EnvironmentPreference::Any,
178                    request: PythonRequest::Directory(root.as_ref().to_owned()),
179                }));
180            }
181            Err(err) => return Err(Error::Discovery(err.into())),
182        }
183
184        if root.as_ref().is_file() {
185            return Err(InvalidEnvironment {
186                path: root.as_ref().to_path_buf(),
187                kind: InvalidEnvironmentKind::NotDirectory,
188            }
189            .into());
190        }
191
192        if root
193            .as_ref()
194            .read_dir()
195            .is_ok_and(|mut dir| dir.next().is_none())
196        {
197            return Err(InvalidEnvironment {
198                path: root.as_ref().to_path_buf(),
199                kind: InvalidEnvironmentKind::Empty,
200            }
201            .into());
202        }
203
204        // Note we do not canonicalize the root path or the executable path, this is important
205        // because the path the interpreter is invoked at can determine the value of
206        // `sys.executable`.
207        let executable = virtualenv_python_executable(&root);
208
209        // If we can't find an executable, exit before querying to provide a better error.
210        if !(executable.is_symlink() || executable.is_file()) {
211            return Err(InvalidEnvironment {
212                path: root.as_ref().to_path_buf(),
213                kind: InvalidEnvironmentKind::MissingExecutable(executable.clone()),
214            }
215            .into());
216        }
217
218        let interpreter = Interpreter::query(executable, cache)?;
219
220        Ok(Self(Arc::new(PythonEnvironmentShared {
221            root: interpreter.sys_prefix().to_path_buf(),
222            interpreter,
223        })))
224    }
225
226    /// Create a [`PythonEnvironment`] from an existing [`PythonInstallation`].
227    pub fn from_installation(installation: PythonInstallation) -> Self {
228        Self::from_interpreter(installation.into_interpreter())
229    }
230
231    /// Create a [`PythonEnvironment`] from an existing [`Interpreter`].
232    pub fn from_interpreter(interpreter: Interpreter) -> Self {
233        Self(Arc::new(PythonEnvironmentShared {
234            root: interpreter.sys_prefix().to_path_buf(),
235            interpreter,
236        }))
237    }
238
239    /// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and `--target` directory.
240    pub fn with_target(self, target: Target) -> std::io::Result<Self> {
241        let inner = Arc::unwrap_or_clone(self.0);
242        Ok(Self(Arc::new(PythonEnvironmentShared {
243            interpreter: inner.interpreter.with_target(target)?,
244            ..inner
245        })))
246    }
247
248    /// Create a [`PythonEnvironment`] from an existing [`Interpreter`] and `--prefix` directory.
249    pub fn with_prefix(self, prefix: Prefix) -> std::io::Result<Self> {
250        let inner = Arc::unwrap_or_clone(self.0);
251        Ok(Self(Arc::new(PythonEnvironmentShared {
252            interpreter: inner.interpreter.with_prefix(prefix)?,
253            ..inner
254        })))
255    }
256
257    /// Returns the root (i.e., `prefix`) of the Python interpreter.
258    pub fn root(&self) -> &Path {
259        &self.0.root
260    }
261
262    /// Return the [`Interpreter`] for this virtual environment.
263    ///
264    /// See also [`PythonEnvironment::into_interpreter`].
265    pub fn interpreter(&self) -> &Interpreter {
266        &self.0.interpreter
267    }
268
269    /// Return the [`PyVenvConfiguration`] for this environment, as extracted from the
270    /// `pyvenv.cfg` file.
271    pub fn cfg(&self) -> Result<PyVenvConfiguration, Error> {
272        Ok(PyVenvConfiguration::parse(self.0.root.join("pyvenv.cfg"))?)
273    }
274
275    /// Set a key-value pair in the `pyvenv.cfg` file.
276    pub fn set_pyvenv_cfg(&self, key: &str, value: &str) -> Result<(), Error> {
277        let content = fs_err::read_to_string(self.0.root.join("pyvenv.cfg"))?;
278        fs_err::write(
279            self.0.root.join("pyvenv.cfg"),
280            PyVenvConfiguration::set(&content, key, value),
281        )?;
282        Ok(())
283    }
284
285    /// Returns `true` if the environment is "relocatable".
286    pub fn relocatable(&self) -> bool {
287        self.cfg().is_ok_and(|cfg| cfg.is_relocatable())
288    }
289
290    /// Returns the location of the Python executable.
291    pub fn python_executable(&self) -> &Path {
292        self.0.interpreter.sys_executable()
293    }
294
295    /// Returns an iterator over the `site-packages` directories inside the environment.
296    ///
297    /// In most cases, `purelib` and `platlib` will be the same, and so the iterator will contain
298    /// a single element; however, in some distributions, they may be different.
299    ///
300    /// Some distributions also create symbolic links from `purelib` to `platlib`; in such cases, we
301    /// still deduplicate the entries, returning a single path.
302    pub fn site_packages(&self) -> impl Iterator<Item = Cow<'_, Path>> {
303        self.0.interpreter.site_packages()
304    }
305
306    /// Returns the path to the `bin` directory inside this environment.
307    pub fn scripts(&self) -> &Path {
308        self.0.interpreter.scripts()
309    }
310
311    /// Grab a file lock for the environment to prevent concurrent writes across processes.
312    pub async fn lock(&self) -> Result<LockedFile, LockedFileError> {
313        self.0.interpreter.lock().await
314    }
315
316    /// Return the [`Interpreter`] for this environment.
317    ///
318    /// See also [`PythonEnvironment::interpreter`].
319    pub fn into_interpreter(self) -> Interpreter {
320        Arc::unwrap_or_clone(self.0).interpreter
321    }
322
323    /// Returns `true` if the [`PythonEnvironment`] uses the same underlying [`Interpreter`].
324    pub fn uses(&self, interpreter: &Interpreter) -> bool {
325        // TODO(zanieb): Consider using `sysconfig.get_path("stdlib")` instead, which
326        // should be generally robust.
327        if cfg!(windows) {
328            // On Windows, we can't canonicalize an interpreter based on its executable path
329            // because the executables are separate shim files (not links). Instead, we
330            // compare the `sys.base_prefix`.
331            let old_base_prefix = self.interpreter().sys_base_prefix();
332            let selected_base_prefix = interpreter.sys_base_prefix();
333            old_base_prefix == selected_base_prefix
334        } else {
335            // On Unix, we can see if the canonicalized executable is the same file.
336            self.interpreter().sys_executable() == interpreter.sys_executable()
337                || same_file::is_same_file(
338                    self.interpreter().sys_executable(),
339                    interpreter.sys_executable(),
340                )
341                .unwrap_or(false)
342        }
343    }
344
345    /// Check if the `pyvenv.cfg` version is the same as the interpreter's Python version.
346    ///
347    /// Returns [`None`] if the versions are the consistent or there is no `pyvenv.cfg`. If the
348    /// versions do not match, returns a tuple of the `pyvenv.cfg` and interpreter's Python versions
349    /// for display.
350    pub fn get_pyvenv_version_conflict(&self) -> Option<(Version, Version)> {
351        let cfg = self.cfg().ok()?;
352        let cfg_version = cfg.version?.into_version();
353
354        // Determine if we should be checking for patch or pre-release equality
355        let exe_version = if cfg_version.release().get(2).is_none() {
356            self.interpreter().python_minor_version()
357        } else if cfg_version.pre().is_none() {
358            self.interpreter().python_patch_version()
359        } else {
360            self.interpreter().python_version().clone()
361        };
362
363        (cfg_version != exe_version).then_some((cfg_version, exe_version))
364    }
365}