diff --git a/pyutilib/component/loader/plugin_importLoader.py b/pyutilib/component/loader/plugin_importLoader.py index a0819f48..66b2e4c9 100644 --- a/pyutilib/component/loader/plugin_importLoader.py +++ b/pyutilib/component/loader/plugin_importLoader.py @@ -13,7 +13,8 @@ __all__ = ['ImportLoader'] from glob import glob -import imp +import importlib.util +import importlib.machinery import re import os import sys @@ -22,6 +23,15 @@ from pyutilib.component.config import ManagedSingletonPlugin from pyutilib.component.core import implements, ExtensionPoint, IIgnorePluginWhenLoading, IPluginLoader, Plugin +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module class ImportLoader(ManagedSingletonPlugin): """Loader that looks for Python source files in the plugins directories, @@ -53,7 +63,7 @@ def load(self, env, search_path, disable_re, name_re): if plugin_name not in sys.modules and name_re.match( plugin_name): try: - module = imp.load_source(plugin_name, plugin_file) + module = load_source(plugin_name, plugin_file) if generate_debug_messages: env.log.debug('Loading file plugin %s from %s' % \ (plugin_name, plugin_file)) diff --git a/pyutilib/misc/import_file.py b/pyutilib/misc/import_file.py index cd5c80ab..052b8735 100644 --- a/pyutilib/misc/import_file.py +++ b/pyutilib/misc/import_file.py @@ -8,7 +8,8 @@ # _________________________________________________________________________ import os -import imp +import importlib +import importlib.machinery import sys import pyutilib.common @@ -24,6 +25,17 @@ runpy_available = False +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def import_file(filename, context=None, name=None, clear_cache=False): """ Import a Python file as a module @@ -125,15 +137,15 @@ def import_file(filename, context=None, name=None, clear_cache=False): else: if dirname is not None: # find_module will return the .py file (never .pyc) - fp, pathname, description = imp.find_module(modulename, - [dirname]) + fp, pathname, description = importlib.find_loader(modulename, + str(dirname)) fp.close() else: try: sys.path.insert(0, implied_dirname) # find_module will return the .py file # (never .pyc) - fp, pathname, description = imp.find_module(modulename) + fp, pathname, description = importlib.find_loader(modulename) fp.close() except ImportError: raise @@ -142,7 +154,7 @@ def import_file(filename, context=None, name=None, clear_cache=False): try: # Note: we are always handing load_source a .py file, but # it will use the .pyc or .pyo file if it exists - module = imp.load_source(name, pathname) + module = load_source(name, pathname) except: et, e, tb = sys.exc_info() import traceback