_importlib.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import sys
  2. def disable_importlib_metadata_finder(metadata):
  3. """
  4. Ensure importlib_metadata doesn't provide older, incompatible
  5. Distributions.
  6. Workaround for #3102.
  7. """
  8. try:
  9. import importlib_metadata
  10. except ImportError:
  11. return
  12. except AttributeError:
  13. import warnings
  14. msg = (
  15. "`importlib-metadata` version is incompatible with `setuptools`.\n"
  16. "This problem is likely to be solved by installing an updated version of "
  17. "`importlib-metadata`."
  18. )
  19. warnings.warn(msg) # Ensure a descriptive message is shown.
  20. raise # This exception can be suppressed by _distutils_hack
  21. if importlib_metadata is metadata:
  22. return
  23. to_remove = [
  24. ob
  25. for ob in sys.meta_path
  26. if isinstance(ob, importlib_metadata.MetadataPathFinder)
  27. ]
  28. for item in to_remove:
  29. sys.meta_path.remove(item)
  30. if sys.version_info < (3, 10):
  31. from setuptools.extern import importlib_metadata as metadata
  32. disable_importlib_metadata_finder(metadata)
  33. else:
  34. import importlib.metadata as metadata # noqa: F401
  35. if sys.version_info < (3, 9):
  36. from setuptools.extern import importlib_resources as resources
  37. else:
  38. import importlib.resources as resources # noqa: F401