From f0d631f39ef0776c26ba51e65dc19e51361fd0ba Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 23 Mar 2026 13:22:09 +0200 Subject: [PATCH] Improve import time of pathlib --- Lib/pathlib/__init__.py | 5 ++--- Lib/test/test_pathlib/test_pathlib.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index 44f967eb12dd4f..a32e4b5320ff6d 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -12,12 +12,13 @@ import posixpath import sys from errno import * -from glob import _StringGlobber, _no_recurse_symlinks from itertools import chain from stat import ( S_IMODE, S_ISDIR, S_ISREG, S_ISLNK, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO, ) from _collections_abc import Sequence +lazy import shutil +lazy from glob import _StringGlobber, _no_recurse_symlinks try: import pwd @@ -1255,8 +1256,6 @@ def _delete(self): if self.is_symlink() or self.is_junction(): self.unlink() elif self.is_dir(): - # Lazy import to improve module import time - import shutil shutil.rmtree(self) else: self.unlink() diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index ef9ea0d11d06a6..f10fcb278b207f 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -84,7 +84,7 @@ def test_is_notimplemented(self): class LazyImportTest(unittest.TestCase): @cpython_only def test_lazy_import(self): - import_helper.ensure_lazy_imports("pathlib", {"shutil"}) + import_helper.ensure_lazy_imports("pathlib", {"glob", "shutil"}) #