Search code examples
pythonpippackaging

pkg_resources.get_distribution("mymodule").version not updated after reload


I'm checking if a package is out of date using

pkg_resources.get_distribution("mymodule").version

If the version isn't matching the latest, I'm running pip install --upgrade git+.... Doing reload('mymodule') is correctly representing the changes, but the above-mentioned snippet still shows the previous version even though the version was bumped in setup.py

I guess the version is only updated per python session? Any other ways of getting live information? Would it be safe to read the latest dist-info directory?


Solution

  • I would give reload(pkg_resources) a try:

    >>> import pkg_resources
    >>> pkg_resources.get_distribution('thing').version
    '0.0.0.dev1'
    >>> # in a different shell session install the new version of thing
    ... 
    >>> pkg_resources.get_distribution('thing').version
    '0.0.0.dev1'
    >>> import importlib
    >>> importlib.reload(pkg_resources)
    <module 'pkg_resources' from '/tmp/tmp.VEueUV76hD/Thing/.tox/develop/lib/python3.6/site-packages/pkg_resources/__init__.py'>
    >>> pkg_resources.get_distribution('thing').version
    '0.0.0.dev2'
    

    See https://github.com/pypa/setuptools/issues/373