Search code examples
pythonpippython-importlib

Difference between version pip show and importlib.metadata.version


I am creating a module, henceforth called mymodule, which I distribute using a pyproject.toml. This file contains a version number. I would like to write this version number in the logfile of mymodule. In mymodule I use the following snippet (in __init__.py) to obtain the version:

import importlib.metadata

__version__ = importlib.metadata.version(__package__)

del importlib.metadata

However this version is wrong. This appears to be the highest version which I have ever installed. For reference the command python3 -m pip show mypackage does actually show the correct version after installing the module locally. I struggle to explain this difference. Can anyone think of a cause of this discrepancy?

I also ran importlib.metadata.version(mypackage) which returned the same incorrect version.


Solution

  • The problem was related to left over build artifacts from using setup.py. importlib and pkg_resources will detect these artifacts in a local installation and pip will not. Deleting the mypackage.egg-info directory fixed the issue.