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