Does ffcx's jit.py need to import importlib.util?

I was caught by some error, that I managed to fix with this:

From 91fc89d46209836b314428c04ba5497b54c1b699 Mon Sep 17 00:00:00 2001
From: edgar <edgar@fenicsproject.discourse.group>
Date: Tue, 28 May 2024
Subject: [RFC PATCH] jit.py: importlib.util

---
 ffcx/codegeneration/jit.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/ffcx/codegeneration/jit.py b/ffcx/codegeneration/jit.py
index 6eb5dbb8f..e73730960 100644
--- a/ffcx/codegeneration/jit.py
+++ b/ffcx/codegeneration/jit.py
@@ -8,6 +8,7 @@
 from __future__ import annotations
 
 import importlib
+import importlib.util
 import io
 import logging
 import os

base-commit: 3b5ae9881e2b3282c15d851dd5965ead5af02518
-- 
2.45.0


If it’s a false alarm, I’m sorry. If it’s useful, I don’t have github, and hope that it’s useful. Thanks.

I think that I have bigger issues (the above patch should still be useful):

  File "/usr/lib/python3.12/site-packages/cffi/_shimmed_dist_utils.py", line 12, in <module>
    import setuptools
  File "/usr/lib/python3.12/site-packages/setuptools/__init__.py", line 7, in <module>
    import _distutils_hack.override  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/_distutils_hack/override.py", line 1, in <module>
    __import__('_distutils_hack').do_override()
  File "/usr/lib/python3.12/site-packages/_distutils_hack/__init__.py", line 77, in do_override
    ensure_local_distutils()
  File "/usr/lib/python3.12/site-packages/_distutils_hack/__init__.py", line 60, in ensure_local_distutils
    importlib.import_module('distutils')
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1322, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1262, in _find_spec
  File "/usr/lib/python3.12/site-packages/_distutils_hack/__init__.py", line 97, in find_spec
    return method()
           ^^^^^^^^
  File "/usr/lib/python3.12/site-packages/_distutils_hack/__init__.py", line 128, in spec_for_distutils
    return importlib.util.spec_from_loader(
           ^^^^^^^^^^^^^^
AttributeError: module 'importlib' has no attribute 'util'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 17, in __PYTHON_EL_eval
  File "examples/my_code.py", line 587, in <module>
    V_cg_vec = fem.functionspace(domain, Ve_cg_vec)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/dolfinx/fem/function.py", line 655, in functionspace
    (ufcx_element, ufcx_dofmap), module, code = jit.ffcx_jit(
                                                ^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/dolfinx/jit.py", line 51, in mpi_jit
    return local_jit(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/dolfinx/jit.py", line 203, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_elements([ufl_object], options=p_ffcx, **p_jit)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/ffcx/codegeneration/jit.py", line 205, in compile_elements
    raise e
  File "/usr/lib/python3.12/site-packages/ffcx/codegeneration/jit.py", line 185, in compile_elements
    impl = _compile_objects(
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/ffcx/codegeneration/jit.py", line 416, in _compile_objects
    ffibuilder.compile(tmpdir=cache_dir, verbose=True, debug=cffi_debug)
  File "/usr/lib/python3.12/site-packages/cffi/api.py", line 725, in compile
    return recompile(self, module_name, source, tmpdir=tmpdir,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/cffi/recompiler.py", line 1546, in recompile
    ext = ffiplatform.get_extension(ext_c_file, module_name, **kwds)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/cffi/ffiplatform.py", line 9, in get_extension
    from cffi._shimmed_dist_utils import Extension
  File "/usr/lib/python3.12/site-packages/cffi/_shimmed_dist_utils.py", line 16, in <module>
    raise Exception("This CFFI feature requires setuptools on Python >= 3.12. The setuptools module is missing or non-functional.") from ex
Exception: This CFFI feature requires setuptools on Python >= 3.12. The setuptools module is missing or non-functional.

I did not expect this, but I had

import importlib.util
# ... code
del importlib.util 

in one of my files. After removing the last line, the sun shone again.