From 3d960bdcaff2a77f7cda3cfa28d1eced5fd0efb2 Mon Sep 17 00:00:00 2001 From: Sondre Sortland Date: Sun, 25 Aug 2019 12:25:53 +0200 Subject: [PATCH] Use importlib for module source loading in Python3 --- python/res/job_queue/ert_script.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/python/res/job_queue/ert_script.py b/python/res/job_queue/ert_script.py index 0efd183fd2..bccbc8728e 100644 --- a/python/res/job_queue/ert_script.py +++ b/python/res/job_queue/ert_script.py @@ -1,5 +1,4 @@ import inspect -import imp import sys import traceback @@ -112,9 +111,25 @@ def outputStackTrace(self, error=None): def loadScriptFromFile(path): """ @rtype: type ErtScript """ try: - m = imp.load_source("ErtScriptModule_%d" % ErtScript.__module_count, path) + module_name = "ErtScriptModule_%d" % ErtScript.__module_count ErtScript.__module_count += 1 - return ErtScript.__findErtScriptImplementations(m) + + py_version = sys.version_info[:2] + if py_version == (2, 7): + import imp + module = imp.load_source(module_name, path) + elif (3, 0) <= py_version < (3, 5): + import importlib.machinery + loader = importlib.machinery.SourceFileLoader(module_name, path) + module = loader.load_module() + elif py_version >= (3, 5): + import importlib.util + spec = importlib.util.spec_from_file_location(module_name, path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + else: + raise Exception("Invalid Python verson {}:".format(py_version)) + return ErtScript.__findErtScriptImplementations(module) except Exception as e: sys.stderr.write("The script '%s' caused an error during load:\n" % path) traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], None)