Skip to content

Commit

Permalink
Replace find_module with find_spec + exec_module
Browse files Browse the repository at this point in the history
find_module is deprecated and 3.10 was removed. The almost immediate
replacement is call to find_spec to get a ModuleSpec and from there,
access to its loader attribute to get a Loader.

importlib's module_from_spec will create the Module and loader's
exec_module will load it (execute it).

For reference:
 - https://bugs.python.org/issue43540
  • Loading branch information
eldipa committed Feb 20, 2022
1 parent f4322d5 commit 9f2a0fe
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion byexample/init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals
import sys, pkgutil, inspect, pprint, os, operator
import importlib.util

from itertools import chain as chain_iters

Expand Down Expand Up @@ -108,7 +109,9 @@ def load_modules(dirnames, cfg):
clog().debug("From '%s' loading '%s'...", path, name)

try:
module = importer.find_module(name).load_module(name)
spec = importer.find_spec(name)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
except Exception as e:
clog().info(
"From '%s' loading '%s'...failed: %s", path, name, str(e)
Expand Down

0 comments on commit 9f2a0fe

Please sign in to comment.