pythonsetuptoolspkg-resources

How do I get a Python distribution URL?


In their setup.py Python packages provides some information. This information can then be found in the PKG_INFO file of the egg.

How can I access them once I have installed the package?

For instance, if I have the following module:

setup(name='myproject',
      version='1.2.0.dev0',
      description='Demo of a setup.py file.',
      long_description=README + "\n\n" + CHANGELOG + "\n\n" + CONTRIBUTORS,
      license='Apache License (2.0)',
      classifiers=[
          "Programming Language :: Python",
          "Programming Language :: Python :: 2",
          "Programming Language :: Python :: 2.7",
          "Programming Language :: Python :: 3",
          "Programming Language :: Python :: 3.4",
          "Programming Language :: Python :: 3.5",
          "Programming Language :: Python :: Implementation :: CPython",
          "Programming Language :: Python :: Implementation :: PyPy",
          "Topic :: Internet :: WWW/HTTP",
          "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
          "License :: OSI Approved :: Apache Software License"
      ],
      keywords="web sync json storage services",
      url='https://github.com/Kinto/kinto')

How can I use Python to get back the information provided in the setup.py?

I was thinking of something similar to that:

import pkg_resource
url = pkg_resource.get_distribution(__package__).url

Any idea?


Solution

  • There is apparently a private API that let you do that with pkg_resources:

    import pkg_resources
    d = pkg_resources.get_distribution(__package__)
    metadata = d._get_metadata(d.PKG_INFO)
    home_page = [m for m in metadata if m.startswith('Home-page:')]
    url = home_page[0].split(':', 1)[1].strip()
    

    I wish we could do better.