Skip to content

Commit 8fb7426

Browse files
committed
Prefer system Python installations over managed ones when --system is used
1 parent 64e91a7 commit 8fb7426

File tree

12 files changed

+50
-17
lines changed

12 files changed

+50
-17
lines changed

crates/uv-dev/src/compile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tracing::info;
55

66
use uv_cache::{Cache, CacheArgs};
77
use uv_configuration::{Concurrency, Preview};
8-
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};
8+
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonPreference, PythonRequest};
99

1010
#[derive(Parser)]
1111
pub(crate) struct CompileArgs {
@@ -25,6 +25,7 @@ pub(crate) async fn compile(args: CompileArgs) -> anyhow::Result<()> {
2525
let interpreter = PythonEnvironment::find(
2626
&PythonRequest::default(),
2727
EnvironmentPreference::OnlyVirtual,
28+
PythonPreference::default(),
2829
&cache,
2930
Preview::default(),
3031
)?

crates/uv-python/src/discovery.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,30 @@ impl PythonPreference {
20932093
Self::Managed | Self::System | Self::OnlyManaged => true,
20942094
}
20952095
}
2096+
2097+
/// Returns a new preference when the `--system` flag is used.
2098+
///
2099+
/// This will convert [`PythonPreference::Managed`] to [`PythonPreference::System`] when system
2100+
/// is set.
2101+
#[must_use]
2102+
pub fn with_system_flag(self, system: bool) -> Self {
2103+
match self {
2104+
// TODO(zanieb): It's not clear if we want to allow `--system` to override
2105+
// `--managed-python`. We should probably make this `from_system_flag` and refactor
2106+
// handling of the `PythonPreference` to use an `Option` so we can tell if the user
2107+
// provided it?
2108+
Self::OnlyManaged => self,
2109+
Self::Managed => {
2110+
if system {
2111+
Self::System
2112+
} else {
2113+
self
2114+
}
2115+
}
2116+
Self::System => self,
2117+
Self::OnlySystem => self,
2118+
}
2119+
}
20962120
}
20972121

20982122
impl PythonDownloads {

crates/uv-python/src/environment.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,16 @@ impl PythonEnvironment {
152152
pub fn find(
153153
request: &PythonRequest,
154154
preference: EnvironmentPreference,
155+
python_preference: PythonPreference,
155156
cache: &Cache,
156157
preview: Preview,
157158
) -> Result<Self, Error> {
158-
let installation = match find_python_installation(
159-
request,
160-
preference,
161-
PythonPreference::default(),
162-
cache,
163-
preview,
164-
)? {
165-
Ok(installation) => installation,
166-
Err(err) => return Err(EnvironmentNotFound::from(err).into()),
167-
};
159+
let installation =
160+
match find_python_installation(request, preference, python_preference, cache, preview)?
161+
{
162+
Ok(installation) => installation,
163+
Err(err) => return Err(EnvironmentNotFound::from(err).into()),
164+
};
168165
Ok(Self::from_installation(installation))
169166
}
170167

crates/uv/src/commands/pip/check.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use uv_cache::Cache;
88
use uv_configuration::Preview;
99
use uv_distribution_types::{Diagnostic, InstalledDist};
1010
use uv_installer::{SitePackages, SitePackagesDiagnostic};
11+
use uv_python::PythonPreference;
1112
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};
1213

1314
use crate::commands::pip::operations::report_target_environment;
@@ -28,6 +29,7 @@ pub(crate) fn pip_check(
2829
let environment = PythonEnvironment::find(
2930
&python.map(PythonRequest::parse).unwrap_or_default(),
3031
EnvironmentPreference::from_system_flag(system, false),
32+
PythonPreference::default().with_system_flag(system),
3133
cache,
3234
preview,
3335
)?;

crates/uv/src/commands/pip/compile.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ pub(crate) async fn pip_compile(
283283

284284
// Find an interpreter to use for building distributions
285285
let environment_preference = EnvironmentPreference::from_system_flag(system, false);
286+
let python_preference = python_preference.with_system_flag(system);
286287
let interpreter = if let Some(python) = python.as_ref() {
287288
let request = PythonRequest::parse(python);
288289
PythonInstallation::find(

crates/uv/src/commands/pip/freeze.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use uv_cache::Cache;
99
use uv_configuration::Preview;
1010
use uv_distribution_types::{Diagnostic, InstalledDist, Name};
1111
use uv_installer::SitePackages;
12+
use uv_python::PythonPreference;
1213
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};
1314

1415
use crate::commands::ExitStatus;
@@ -30,6 +31,7 @@ pub(crate) fn pip_freeze(
3031
let environment = PythonEnvironment::find(
3132
&python.map(PythonRequest::parse).unwrap_or_default(),
3233
EnvironmentPreference::from_system_flag(system, false),
34+
PythonPreference::default().with_system_flag(system),
3335
cache,
3436
preview,
3537
)?;

crates/uv/src/commands/pip/install.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub(crate) async fn pip_install(
193193
.map(PythonRequest::parse)
194194
.unwrap_or_default(),
195195
EnvironmentPreference::from_system_flag(system, false),
196-
python_preference,
196+
python_preference.with_system_flag(system),
197197
&cache,
198198
preview,
199199
)?;
@@ -206,6 +206,7 @@ pub(crate) async fn pip_install(
206206
.map(PythonRequest::parse)
207207
.unwrap_or_default(),
208208
EnvironmentPreference::from_system_flag(system, true),
209+
PythonPreference::default().with_system_flag(system),
209210
&cache,
210211
preview,
211212
)?;

crates/uv/src/commands/pip/list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use uv_installer::SitePackages;
2525
use uv_normalize::PackageName;
2626
use uv_pep440::Version;
2727
use uv_python::PythonRequest;
28-
use uv_python::{EnvironmentPreference, PythonEnvironment};
28+
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonPreference};
2929
use uv_resolver::{ExcludeNewer, PrereleaseMode};
3030

3131
use crate::commands::ExitStatus;
@@ -65,6 +65,7 @@ pub(crate) async fn pip_list(
6565
let environment = PythonEnvironment::find(
6666
&python.map(PythonRequest::parse).unwrap_or_default(),
6767
EnvironmentPreference::from_system_flag(system, false),
68+
PythonPreference::default().with_system_flag(system),
6869
cache,
6970
preview,
7071
)?;

crates/uv/src/commands/pip/show.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use uv_fs::Simplified;
1313
use uv_install_wheel::read_record_file;
1414
use uv_installer::SitePackages;
1515
use uv_normalize::PackageName;
16-
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest};
16+
use uv_python::{EnvironmentPreference, PythonEnvironment, PythonPreference, PythonRequest};
1717

1818
use crate::commands::ExitStatus;
1919
use crate::commands::pip::operations::report_target_environment;
@@ -47,6 +47,7 @@ pub(crate) fn pip_show(
4747
let environment = PythonEnvironment::find(
4848
&python.map(PythonRequest::parse).unwrap_or_default(),
4949
EnvironmentPreference::from_system_flag(system, false),
50+
PythonPreference::default().with_system_flag(system),
5051
cache,
5152
preview,
5253
)?;

crates/uv/src/commands/pip/sync.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub(crate) async fn pip_sync(
170170
.map(PythonRequest::parse)
171171
.unwrap_or_default(),
172172
EnvironmentPreference::from_system_flag(system, false),
173-
python_preference,
173+
python_preference.with_system_flag(system),
174174
&cache,
175175
preview,
176176
)?;
@@ -183,6 +183,7 @@ pub(crate) async fn pip_sync(
183183
.map(PythonRequest::parse)
184184
.unwrap_or_default(),
185185
EnvironmentPreference::from_system_flag(system, true),
186+
PythonPreference::default().with_system_flag(system),
186187
&cache,
187188
preview,
188189
)?;

0 commit comments

Comments
 (0)