[reportlab-users] Deprecated warning on Python 3.10

Frank Epperlein frank+reportlab-users at epperle.in
Sat Sep 30 01:53:23 EDT 2023


Hi Robin,

first I have to say that for me it is surprisingly still working with python 3.12.0rc3 - even with the old code - so for whatever reason they did not remove the import_module function yet. I still get only the warning.

I tested your code change in 18ae86d14b0a and it resolves the issue for me.

I also tested if the change removes the warning in pytest (what the author of the original mail complained about) and it is also solved there.

Thank you! :)

We do not need the utils.py import, so I cannot really test it. Anyhow in our codebase we have similar usages and there we solve it like this:

--- a/lib/utils.py
+++ b/lib/utils.py
@@ -581,7 +581,9 @@
         path = os.path.join(dir,name+ext)
         if os.path.isfile(path):
             spec = importlib_util.spec_from_file_location(name,path)
-            return spec.loader.load_module()
+            module = importlib_util.module_from_spec(spec)
+            spec.loader.exec_module(module)
+            return module
     raise ImportError('no suitable file found')

 def rl_get_module(name,dir):
@@ -596,11 +598,12 @@
         except:
             if isCompactDistro():
                 #attempt a load from inside the zip archive
-                import zipimport
                 dir = _startswith_rl(dir)
                 dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep))
-                zi = zipimport.zipimporter(dir)
-                return zi.load_module(name)
+                spec = importlib_util.spec_from_file_location(name, dir)
+                module = importlib_util.module_from_spec(spec)
+                spec.loader.exec_module(module)
+                return module
             raise ImportError('%s[%s]' % (name,dir))
     finally:
         if om: sys.modules[name] = om


Note that the zipimport is, as far as I know, not really needed. The import lib should be able to solve this itself. So this probably needs a cleanup anyway. But as I said I cannot test it. It is completely artificial.

Best and thank you for the fix already.

Frank



More information about the reportlab-users mailing list