Fix Python 3.12 compatibility - use importlib.util.find_spec("numpy") instead of imp
Hi!
In the pip distribution (at least on windows), in flatbuffers\compat.py, it uses imp, which is removed in python 3.12, so I had to monkey patch it to use importlib instead to make the library work properly.
I couldn't find that code anywhere, so I suppose it is in some pip package source somewhere, otherwise I'd made a PR.
Any update on this? It is only used for ;
def import_numpy():
"""
Returns the numpy module if it exists on the system,
otherwise returns None.
"""
try:
imp.find_module('numpy')
numpy_exists = True
except ImportError:
numpy_exists = False
if numpy_exists:
# We do this outside of try/except block in case numpy exists
# but is not installed correctly. We do not want to catch an
# incorrect installation which would manifest as an
# ImportError.
import numpy as np
else:
np = None
return np
and that doesn't seem as a vital thing?
I made the substitution import imp with import importlib.util and imp.find_module('numpy') with importlib.util.find_spec("numpy") in compat.py and then the code worked with python 3.12. This is a kludgy fix to have to make.
@ozkulah where is the code example you've shown? I can't find it in this repo. This problem is preventing the Ultralytics repo from migrating our CI to Python 3.12 https://github.com/ultralytics/ultralytics/actions/runs/14646481197/job/41101863180?pr=16413 from PR https://github.com/ultralytics/ultralytics/pull/16413