Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Use importlib for module source loading in Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreso committed Aug 30, 2019
1 parent 5c88920 commit 3d960bd
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions python/res/job_queue/ert_script.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import inspect
import imp
import sys
import traceback

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3d960bd

Please sign in to comment.