Skip to content

Commit bd9154a

Browse files
authored
bpo-41718: runpy now imports pkgutil in functions (GH-24996)
Reduce the number of modules imported by "python3 -m module". The runpy module no longer imports at startup (in the module body), but only in functions using it: _get_code_from_file() and run_path().
1 parent d72e8d4 commit bd9154a

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Lib/runpy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io
1717
import types
1818
import os
19-
from pkgutil import read_code, get_importer
2019

2120
__all__ = [
2221
"run_module", "run_path",
@@ -233,6 +232,7 @@ def _get_main_module_details(error=ImportError):
233232

234233
def _get_code_from_file(run_name, fname):
235234
# Check for a compiled file first
235+
from pkgutil import read_code
236236
decoded_path = os.path.abspath(os.fsdecode(fname))
237237
with io.open_code(decoded_path) as f:
238238
code = read_code(f)
@@ -255,6 +255,7 @@ def run_path(path_name, init_globals=None, run_name=None):
255255
if run_name is None:
256256
run_name = "<run_path>"
257257
pkg_name = run_name.rpartition(".")[0]
258+
from pkgutil import get_importer
258259
importer = get_importer(path_name)
259260
# Trying to avoid importing imp so as to not consume the deprecation warning.
260261
is_NullImporter = False

0 commit comments

Comments
 (0)