You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Possible lazy import opportunities in pathlib #77621
Due to a python-ideas discussion about reducing boilerplate for __file__-relative path calculations, I was running "./python -X importtime -S -c 'import pathlib'" and noticed three potential candidates where it may be worthwhile deferring the imports until the modules are actually needed:
re (used in _WildcardSelector)
fnmatch (used in PurePath.match and _WildcardSelector)
urllib.parse (used in PurePath.as_uri, by way of self._flavour.make_uri)
Using an optimised Python 3.7 on an SSD with warm disk caches, commenting out those 3 imports reduced my typical import times for pathlib from 12-13 ms to 7-8 ms)
Personally I support moving imports of rarely used modules into functions that need them. But this is against PEP-8 and should be discussed more widely. Perhaps this rule should be weaken, we have to state what exceptions are permitted.
Users probably shouldn't pay the cost of importing warnings unless they're actually using a deprecated function. There are quite a few uses of warnings in pathlib, though, which is why I held off from changing warnings to be a lazy import in pathlib as part of #110286 -- maybe making it a lazy import in pathlib just adds too much ugliness for it to be worthwhile.
The import-time speedup from making warnings a lazy import is small, but measurable.
Activity
ncoghlan commentedon May 7, 2018
Due to a python-ideas discussion about reducing boilerplate for __file__-relative path calculations, I was running "./python -X importtime -S -c 'import pathlib'" and noticed three potential candidates where it may be worthwhile deferring the imports until the modules are actually needed:
Using an optimised Python 3.7 on an SSD with warm disk caches, commenting out those 3 imports reduced my typical import times for pathlib from 12-13 ms to 7-8 ms)
njsmith commentedon May 7, 2018
Also it's a smaller win, but it might be worth considering whether we can avoid the import of 'nt' on posix platforms and vice-versa.
serhiy-storchaka commentedon May 14, 2018
Did you mean the idea about module-level alias to dirname(file)? I think this is a dead horse. See bpo-33277.
serhiy-storchaka commentedon May 14, 2018
Personally I support moving imports of rarely used modules into functions that need them. But this is against PEP-8 and should be discussed more widely. Perhaps this rule should be weaken, we have to state what exceptions are permitted.
barneygale commentedon Nov 18, 2023
I've moved the
urllibimports intoas_uri()/from_uri()as their use is uncommon. Everything else is likely to be hit in general usage, I think.AlexWaygood commentedon Nov 18, 2023
You could probably make this one lazy:
cpython/Lib/pathlib.py
Line 17 in 607b5e3
Users probably shouldn't pay the cost of importing
warningsunless they're actually using a deprecated function. There are quite a few uses ofwarningsinpathlib, though, which is why I held off from changingwarningsto be a lazy import inpathlibas part of #110286 -- maybe making it a lazy import inpathlibjust adds too much ugliness for it to be worthwhile.The import-time speedup from making
warningsa lazy import is small, but measurable.barneygale commentedon Nov 18, 2023
Three of four uses of
warningswill be removed in 3.14, so perhaps we could look again whenmainbecomes 3.14?AlexWaygood commentedon Nov 18, 2023
SGTM!
pythonGH-77621: Delay some imports from pathlib
GH-77621: Delay some imports from pathlib (#112244)
barneygale commentedon Dec 23, 2023
Resolving this issue as I believe there's nothing left to do!
urllibimport was made lazy in GH-107465: Addpathlib.Path.from_uri()classmethod. #107640fnmatch,globandreimports were made lazy in GH-77621: Delay some imports from pathlib #112244warningsimport will be removed when we branch for Python 3.14pythonGH-77621: Delay some imports from pathlib (python#112244)
pythonGH-77621: Delay some imports from pathlib (python#112244)