Just try PEP810, I make it combine with my project “friendly_module_not_found_error”. In this case, is the note “Don’t import any modules in the method ‘__find__’” in ModuleNotFoundError right?
>>> import sys
>>> lazy import art
>>> class WrongHook:
... def find_spec(*args, **kwargs):...
... def __find__(self, name=None): return art.a
...
>>> a = WrongHook()
>>> sys.meta_path.append(a)
>>> art
Traceback (most recent call last):
File "<python-input-12>", line 1, in <module>
lazy import art
ImportError: deferred import of 'art' raised an exception during resolution
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<python-input-16>", line 1, in <module>
art
ModuleNotFoundError: No module named 'art'. Did you mean: 'ast'?
ImportError found in 'WrongHook.__find__' module '__main__':
File "<python-input-13>", line 3, in __find__
Don't import any modules in the method '__find__'