Skip to content

Commit 79e0446

Browse files
zaniebastral-automations-bot[bot]
authored andcommitted
Stabilize no-distutils-patch (astral-sh#22096)
## Summary New virtual environments on Python 3.10 and later omit `_virtualenv.py` and `_virtualenv.pth` by default. Python 3.9 and earlier retain the distutils configuration patch. Existing environments are left alone until recreated. Remove the preview gate, feature declaration, and unused `uv-virtualenv` preview dependency, and add the 0.13 migration note. The existing unknown-feature handling allows old `--preview-features no-distutils-patch` invocations to continue with a warning. Cover the 3.9/3.10 boundary, seeded environments, the removed feature name, and recreation of existing environments. Fixes astral-sh#20791. Targets `release/0.13.0` for astral-sh#22092.
1 parent d80a95b commit 79e0446

11 files changed

Lines changed: 128 additions & 27 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ Unreleased.
99

1010
### Breaking changes
1111

12+
- **Omit the distutils startup patch on Python 3.10 and later**
13+
([#22096](https://github.com/astral-sh/uv/pull/22096))
14+
15+
Previously, uv installed `_virtualenv.py` and `_virtualenv.pth` into every new virtual environment
16+
to prevent distutils configuration from changing installation paths. Now, uv omits these files on
17+
Python 3.10 and later, which already ignore the affected configuration keys. This reduces Python
18+
startup overhead. Python 3.9 and earlier retain the patch.
19+
20+
You cannot opt out of this behavior. Existing virtual environments are not modified automatically.
21+
Recreate an environment with Python 3.10 or later to remove the patch.
22+
23+
This stabilizes the `no-distutils-patch` preview feature.
24+
1225
## 0.12.x
1326

1427
See [changelogs/0.12.x](./changelogs/0.12.x.md)

‎Cargo.lock‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/uv-dev/src/generate_preview_features_reference.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ mod tests {
145145
- <a id="missing-exclude-newer-package-lock" href="#missing-exclude-newer-package-lock"><code>missing-exclude-newer-package-lock</code></a>: Exclude `exclude-newer-package` entries from the lockfile when not included in the
146146
project's resolved dependencies.
147147
- <a id="native-auth" href="#native-auth"><code>native-auth</code></a>: Enables storage of credentials in a [system-native location](../concepts/authentication/http.md#the-uv-credentials-store).
148-
- <a id="no-distutils-patch" href="#no-distutils-patch"><code>no-distutils-patch</code></a>: Stops installing the `_virtualenv.py` / `_virtualenv.pth` distutils configuration monkeypatch
149-
in virtual environments for Python 3.10 and later.
150148
- <a id="package-conflicts" href="#package-conflicts"><code>package-conflicts</code></a>: Allows defining workspace conflicts at the package level.
151149
- <a id="packaged-init" href="#packaged-init"><code>packaged-init</code></a>: Makes `uv init` create a packaged application with a `src/` layout, build system, and script
152150
entry point by default.

‎crates/uv-preview/src/lib.rs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,6 @@ pub enum PreviewFeature {
318318
ToolInstallLocks,
319319
/// Allows using `uv workspace list --scripts`.
320320
WorkspaceListScripts,
321-
/// Stops installing the `_virtualenv.py` / `_virtualenv.pth` distutils configuration monkeypatch
322-
/// in virtual environments for Python 3.10 and later.
323-
NoDistutilsPatch,
324321
/// Allows requiring a hash algorithm for configured package indexes.
325322
IndexHashAlgorithm,
326323
/// Rejects non-canonical lockfile formatting when using `--locked` or `--check`.

‎crates/uv-virtualenv/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ uv-console = { workspace = true }
2525
uv-errors = { workspace = true }
2626
uv-fs = { workspace = true }
2727
uv-platform-tags = { workspace = true }
28-
uv-preview = { workspace = true }
2928
uv-pypi-types = { workspace = true }
3029
uv-shell = { workspace = true }
3130
uv-version = { workspace = true }

‎crates/uv-virtualenv/src/virtualenv.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use tracing::{debug, trace};
1717
use crate::{Error, Prompt};
1818
use uv_fs::{CWD, PythonExt, Simplified, cachedir};
1919
use uv_platform_tags::Os;
20-
use uv_preview::PreviewFeature;
2120
use uv_pypi_types::Scheme;
2221
use uv_python_interpreter::{Interpreter, VirtualEnvironment};
2322
use uv_python_managed::{
@@ -50,7 +49,6 @@ const VIRTUALENV_PATCH: &str = include_str!("_virtualenv.py");
5049
/// See <https://github.com/pypa/virtualenv/issues/3181>
5150
fn install_distutils_patch(interpreter: &Interpreter) -> bool {
5251
interpreter.python_tuple() < (3, 10)
53-
|| !uv_preview::is_enabled(PreviewFeature::NoDistutilsPatch)
5452
}
5553

5654
/// Very basic `.cfg` file format writer.

‎crates/uv/tests/pip/pip_sync.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,7 +3271,7 @@ fn compile() -> Result<()> {
32713271
Resolved 1 package in [TIME]
32723272
Prepared 1 package in [TIME]
32733273
Installed 1 package in [TIME]
3274-
Bytecode compiled 3 files in [TIME]
3274+
Bytecode compiled 2 files in [TIME]
32753275
+ markupsafe==2.1.3
32763276
"
32773277
);
@@ -3317,7 +3317,7 @@ fn recompile() -> Result<()> {
33173317
exit_code: 0 (success)
33183318
----- stderr -----
33193319
Resolved 1 package in [TIME]
3320-
Bytecode compiled 3 files in [TIME]
3320+
Bytecode compiled 2 files in [TIME]
33213321
"
33223322
);
33233323

‎crates/uv/tests/pip_install/pip_install.rs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,6 @@ dependencies = ["flask==1.0.x"]
966966
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
967967
File "[CACHE_DIR]/builds-v0/[TMP]/[PYTHON-LIB]/site-packages/setuptools/_distutils/core.py", line 159, in setup
968968
dist.parse_config_files()
969-
File "[CACHE_DIR]/builds-v0/[TMP]/[PYTHON-LIB]/site-packages/_virtualenv.py", line 21, in parse_config_files
970-
result = old_parse_config_files(self, *args, **kwargs)
971-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
972969
File "[CACHE_DIR]/builds-v0/[TMP]/[PYTHON-LIB]/site-packages/setuptools/dist.py", line 631, in parse_config_files
973970
pyprojecttoml.apply_configuration(self, filename, ignore_option_errors)
974971
File "[CACHE_DIR]/builds-v0/[TMP]/[PYTHON-LIB]/site-packages/setuptools/config/pyprojecttoml.py", line 68, in apply_configuration

‎crates/uv/tests/python/venv.rs‎

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,40 +177,36 @@ fn create_venv_caches_upgradeable_interpreter() -> Result<()> {
177177
}
178178

179179
#[test]
180-
fn create_venv_preview_skips_distutils_patch_on_py310_plus() {
181-
let context = uv_test::test_context_with_versions!(&["3.12"]);
180+
fn create_venv_skips_distutils_patch_on_py310() {
181+
let context = uv_test::test_context_with_versions!(&["3.10"]);
182182

183183
uv_snapshot!(context.filters(), context.venv()
184184
.arg(context.venv.as_os_str())
185185
.arg("--python")
186-
.arg("3.12")
187-
.arg("--preview-features")
188-
.arg("no-distutils-patch"), @"
186+
.arg("3.10"), @"
189187
exit_code: 0 (success)
190188
----- stderr -----
191-
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
189+
Using CPython 3.10.[X] interpreter at: [PYTHON-3.10]
192190
Creating virtual environment at: .venv
193191
Activate with: source .venv/[BIN]/activate
194192
"
195193
);
196194

197195
context.venv.assert(predicates::path::is_dir());
198-
let site_packages = site_packages_path(context.venv.path(), "python3.12");
196+
let site_packages = site_packages_path(context.venv.path(), "python3.10");
199197
assert!(!site_packages.join("_virtualenv.py").exists());
200198
assert!(!site_packages.join("_virtualenv.pth").exists());
201199
}
202200

203201
#[test]
204202
#[cfg(feature = "test-python-eol")]
205-
fn create_venv_preview_keeps_distutils_patch_on_py39() {
203+
fn create_venv_keeps_distutils_patch_on_py39() {
206204
let context = uv_test::test_context_with_versions!(&["3.9"]);
207205

208206
uv_snapshot!(context.filters(), context.venv()
209207
.arg(context.venv.as_os_str())
210208
.arg("--python")
211-
.arg("3.9")
212-
.arg("--preview-features")
213-
.arg("no-distutils-patch"), @"
209+
.arg("3.9"), @"
214210
exit_code: 0 (success)
215211
----- stderr -----
216212
Using CPython 3.9.[X] interpreter at: [PYTHON-3.9]
@@ -225,6 +221,61 @@ fn create_venv_preview_keeps_distutils_patch_on_py39() {
225221
assert!(site_packages.join("_virtualenv.pth").is_file());
226222
}
227223

224+
#[test]
225+
fn create_venv_with_removed_distutils_patch_preview() {
226+
let context = uv_test::test_context_with_versions!(&["3.10"]);
227+
228+
uv_snapshot!(context.filters(), context.venv()
229+
.arg("--python")
230+
.arg("3.10")
231+
.arg("--preview-features")
232+
.arg("no-distutils-patch"), @"
233+
exit_code: 0 (success)
234+
----- stderr -----
235+
warning: Unknown preview feature: `no-distutils-patch`
236+
Using CPython 3.10.[X] interpreter at: [PYTHON-3.10]
237+
Creating virtual environment at: .venv
238+
Activate with: source .venv/[BIN]/activate
239+
");
240+
241+
let site_packages = site_packages_path(context.venv.path(), "python3.10");
242+
assert!(!site_packages.join("_virtualenv.py").exists());
243+
assert!(!site_packages.join("_virtualenv.pth").exists());
244+
}
245+
246+
#[test]
247+
fn create_venv_preserves_existing_distutils_patch() -> Result<()> {
248+
let context = uv_test::test_context!("3.10");
249+
let site_packages = site_packages_path(context.venv.path(), "python3.10");
250+
fs_err::write(site_packages.join("_virtualenv.py"), "")?;
251+
fs_err::write(site_packages.join("_virtualenv.pth"), "import _virtualenv")?;
252+
253+
uv_snapshot!(context.filters(), context.venv()
254+
.arg("--allow-existing")
255+
.arg("--python")
256+
.arg("3.10"), @"
257+
exit_code: 0 (success)
258+
----- stderr -----
259+
Using CPython 3.10.[X] interpreter at: [PYTHON-3.10]
260+
Creating virtual environment at: .venv
261+
Activate with: source .venv/[BIN]/activate
262+
");
263+
264+
assert!(site_packages.join("_virtualenv.py").is_file());
265+
assert!(site_packages.join("_virtualenv.pth").is_file());
266+
267+
context
268+
.venv()
269+
.arg("--clear")
270+
.arg("--python")
271+
.arg("3.10")
272+
.assert()
273+
.success();
274+
assert!(!site_packages.join("_virtualenv.py").exists());
275+
assert!(!site_packages.join("_virtualenv.pth").exists());
276+
Ok(())
277+
}
278+
228279
#[test]
229280
fn create_centralized_project_environment_bypasses() -> Result<()> {
230281
let context = uv_test::test_context_with_versions!(&["3.12"]);
@@ -1334,6 +1385,9 @@ fn seed() {
13341385
);
13351386

13361387
context.venv.assert(predicates::path::is_dir());
1388+
let site_packages = site_packages_path(context.venv.path(), "python3.12");
1389+
assert!(!site_packages.join("_virtualenv.py").exists());
1390+
assert!(!site_packages.join("_virtualenv.pth").exists());
13371391
}
13381392

13391393
#[test]
@@ -1359,6 +1413,54 @@ fn seed_older_python_version() {
13591413
context.venv.assert(predicates::path::is_dir());
13601414
}
13611415

1416+
#[test]
1417+
#[cfg(feature = "test-pypi")]
1418+
fn seed_skips_distutils_patch_on_py310() {
1419+
let context = uv_test::test_context_with_versions!(&["3.10"]);
1420+
1421+
uv_snapshot!(context.filters(), context.venv()
1422+
.arg("--seed")
1423+
.arg("--python")
1424+
.arg("3.10"), @"
1425+
exit_code: 0 (success)
1426+
----- stderr -----
1427+
Using CPython 3.10.[X] interpreter at: [PYTHON-3.10]
1428+
Creating virtual environment with seed packages at: .venv
1429+
+ pip==24.0
1430+
+ setuptools==69.2.0
1431+
+ wheel==0.43.0
1432+
Activate with: source .venv/[BIN]/activate
1433+
");
1434+
1435+
let site_packages = site_packages_path(context.venv.path(), "python3.10");
1436+
assert!(!site_packages.join("_virtualenv.py").exists());
1437+
assert!(!site_packages.join("_virtualenv.pth").exists());
1438+
}
1439+
1440+
#[test]
1441+
#[cfg(all(feature = "test-pypi", feature = "test-python-eol"))]
1442+
fn seed_keeps_distutils_patch_on_py39() {
1443+
let context = uv_test::test_context_with_versions!(&["3.9"]);
1444+
1445+
uv_snapshot!(context.filters(), context.venv()
1446+
.arg("--seed")
1447+
.arg("--python")
1448+
.arg("3.9"), @"
1449+
exit_code: 0 (success)
1450+
----- stderr -----
1451+
Using CPython 3.9.[X] interpreter at: [PYTHON-3.9]
1452+
Creating virtual environment with seed packages at: .venv
1453+
+ pip==24.0
1454+
+ setuptools==69.2.0
1455+
+ wheel==0.43.0
1456+
Activate with: source .venv/[BIN]/activate
1457+
");
1458+
1459+
let site_packages = site_packages_path(context.venv.path(), "python3.9");
1460+
assert!(site_packages.join("_virtualenv.py").is_file());
1461+
assert!(site_packages.join("_virtualenv.pth").is_file());
1462+
}
1463+
13621464
#[test]
13631465
fn create_venv_with_invalid_http_timeout() {
13641466
let context = uv_test::test_context_with_versions!(&["3.12"]).with_http_timeout("not_a_number");

‎crates/uv/tests/sync/show_settings.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4017,7 +4017,6 @@ fn preview_features() {
40174017
+ CentralizedProjectEnvs,
40184018
+ ToolInstallLocks,
40194019
+ WorkspaceListScripts,
4020-
+ NoDistutilsPatch,
40214020
+ IndexHashAlgorithm,
40224021
+ LockfileFormatCheck,
40234022
+ LockfileNormalization,

0 commit comments

Comments
 (0)
⌂ 换网址