Dear python enthusiasts,
For some industrial project a few years ago we needed a reliable way to get the version number for a package or module. I found out that there was actually no method working in all edge cases, including:
So I created one, and finally found the time to publish it. No rocket science here, but you may find this new package useful: https://smarie.github.io/python-getversion/
It works with any imported module for now, including submodules. Along with the version you get details about why a given version number is returned (is it because of the __version__ attribute that was found, or because of the Version metadata in the installed package, etc.) Also, if one edge case is missing, it is fairly easy to add it.
If I missed something in the stdlib (I acme across the importlib.metadata initiative but as of now it does not seem to cover all of the above cases), please let me know so that I can cite it in the documentation and even redirect to it if it happens to already cover all the cases.