python / importlib_metadata

Library to access metadata for Python packages
https://importlib-metadata.readthedocs.io
Apache License 2.0
123 stars 81 forks source link

Q: how to replace `__import__("pkg_resources").declare_namespace(__name__)` by some code using `importlib_metadata`? #378

Closed kloczek closed 2 years ago

kloczek commented 2 years ago

I'm trying to get rid of usage 'pkg_resourcesin allsphinxcontrib-` packages. All those packages have in source tree something like

$ cat sphinxcontrib/__init__.py
__import__("pkg_resources").declare_namespace(__name__)

I cannot figure out how to replace that to use importlib_metadata. May I ask for help? 🤔

jaraco commented 2 years ago

Unfortunately, there's no importlib replacement for declare_namespace. That technique is part of how sphinx has specified the namespace for contrib packages. The only way to eliminate that usage of pkg_resources is to switch to a different technique for namespace packages. Sadly, that will require a coordinated effort of all authors of sphinxcontrib packages and likely backward-incompatible transition because pkg_resource-style namespace packages are incompatible with pkgutil-style and native namespace packages.

I did this same transition with the jaraco namespace in early 2019. The good news is that it was mostly painless, but I also was able to update all the packages in a short period (days/weeks), so any compatibility issues that might have arisen would have been minimized.

Your best bet would be to contact the Sphinx maintainers and see if they have any plans to migrate to pkgutil or native namespace packages.

See the docs on namespace packages.

I wish I had a better answer, but I don't think there's anything that importlib can do to help here.

kloczek commented 2 years ago

I really appreciate all those details. Thank you very much for your time😃