From 46b29f0307f7db376da62e7c122c8725df54fe54 Mon Sep 17 00:00:00 2001 From: Evan Blaudy Date: Tue, 10 Oct 2023 02:04:30 +0200 Subject: [PATCH] Stop using imp module it's deprecated since python 3.4 and removed in python 3.12 Signed-off-by: Evan Blaudy --- .../opentimelineio/plugins/python_plugin.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/py-opentimelineio/opentimelineio/plugins/python_plugin.py b/src/py-opentimelineio/opentimelineio/plugins/python_plugin.py index 7a5d3be27a..e5b80979fc 100644 --- a/src/py-opentimelineio/opentimelineio/plugins/python_plugin.py +++ b/src/py-opentimelineio/opentimelineio/plugins/python_plugin.py @@ -4,10 +4,10 @@ """Base class for OTIO plugins that are exposed by manifests.""" import os -import imp import inspect import collections import copy +import importlib.util from .. import ( core, @@ -107,21 +107,23 @@ def module_abs_path(self): def _imported_module(self, namespace): """Load the module this plugin points at.""" - pyname = os.path.splitext(os.path.basename(self.module_abs_path()))[0] - pydir = os.path.dirname(self.module_abs_path()) - - (file_obj, pathname, description) = imp.find_module(pyname, [pydir]) - - with file_obj: - # this will reload the module if it has already been loaded. - mod = imp.load_module( - f"opentimelineio.{namespace}.{self.name}", - file_obj, - pathname, - description + module_name = f"opentimelineio.{namespace}.{self.name}" + try: + # Attempt to import the module using importlib first + mod = importlib.import_module(module_name) + except ImportError: + # If the module couldn't be imported, import it manually + pyname = os.path.splitext(os.path.basename(self.module_abs_path()))[0] + pydir = os.path.dirname(self.module_abs_path()) + spec = importlib.util.spec_from_file_location( + module_name, + os.path.join(pydir, f"{pyname}.py"), ) - return mod + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + + return mod def module(self): """Return the module object for this adapter. """