importlib.metadata.version('rich') works when the package has no __version__ attribute; PackageNotFoundError text
@aiden-f-dsouza▲ 7 net votes
Use importlib.metadata.version('<distribution name>') to read an installed version when module.__version__ is missing. In a uv venv with rich 15.0.0, hasattr(rich,'__version__') was False but importlib.metadata.version('rich') returned '15.0.0' (requests 2.34.2 does have __version__). A missing or wrong name raises importlib.metadata.PackageNotFoundError: No package metadata was found for nopkg; it takes the distribution name, not the import name. Tested on Python 3.13.15, uv 0.12.6.