Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9366df

Browse files
[3.12] gh-104212: Explain how to port imp.load_source() (GH-105978) (#106083)
gh-104212: Explain how to port imp.load_source() (GH-105978) Explain how to port removed imp.load_source() to importlib in What's New in Python 3.12. (cherry picked from commit 18a7c86) Co-authored-by: Victor Stinner <[email protected]>
1 parent f955ed9 commit e9366df

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Doc/whatsnew/3.12.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,21 @@ Removed
13851385
``imp.source_from_cache()`` :func:`importlib.util.source_from_cache`
13861386
================================= =======================================
13871387

1388+
* Replace ``imp.load_source()`` with::
1389+
1390+
import importlib.util
1391+
import importlib.machinery
1392+
1393+
def load_source(modname, filename):
1394+
loader = importlib.machinery.SourceFileLoader(modname, filename)
1395+
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
1396+
module = importlib.util.module_from_spec(spec)
1397+
# The module is always executed and not cached in sys.modules.
1398+
# Uncomment the following line to cache the module.
1399+
# sys.modules[module.__name__] = module
1400+
loader.exec_module(module)
1401+
return module
1402+
13881403
* Removed :mod:`!imp` functions and attributes with no replacements:
13891404

13901405
* undocumented functions:
@@ -1393,7 +1408,6 @@ Removed
13931408
* ``imp.load_compiled()``
13941409
* ``imp.load_dynamic()``
13951410
* ``imp.load_package()``
1396-
* ``imp.load_source()``
13971411

13981412
* ``imp.lock_held()``, ``imp.acquire_lock()``, ``imp.release_lock()``:
13991413
the locking scheme has changed in Python 3.3 to per-module locks.

0 commit comments

Comments
 (0)