Hello folks. We are upgrading to Python 3.12 in Fedora and I need to port various packages I haven’t written from imp
to importlib
.
This and an example of such code (from dblatex):
import imp
def load(modname):
try:
file, path, descr = imp.find_module(modname, [""])
except ImportError:
try:
file, path, descr = imp.find_module(modname,
[os.path.dirname(__file__)])
except ImportError:
raise ValueError("Xslt '%s' not found" % modname)
mod = imp.load_module(modname, file, path, descr)
file.close()
...
This is what I get (obviously):
Traceback...
import imp
ModuleNotFoundError: No module named 'imp'
First, I look at What’s New In Python 3.12 — Python 3.12.1 documentation. It says:
The
imp
module has been removed. (Contributed by Barry Warsaw in gh-98040.)
The Porting to Python 3.12 is silent about this.
Looking at the Python 3.11 documentation for imp imp — Access the import internals — Python 3.11.7 documentation I looked up the find_module function. It says:
Deprecated since version 3.3: Use
importlib.util.find_spec()
instead unless Python 3.3 compatibility is required, in which case useimportlib.find_loader()
. For example usage of the former case, see the Examples section of theimportlib
documentation.
But neither of that functions or examples sets its custom path
. The only thing that seems to accept path
is also deprecated.
Where do I find guidance on how to move from the simple (but apparently evil) imp
module?