I want to find the distribution of a file. The file should discovery its own distribution.
I tried this, but this does not work:
import os
import pkg_resources
for dist in pkg_resources.find_distributions(os.path.dirname(__file__)):
print dist
The file with the of the above was installed using pip install -e ...
.
I could not find a solution in the docs:
https://pythonhosted.org/setuptools/pkg_resources.html#distribution-objects
The solution should not contain the string of the package. It should be generic.
The pkg_resources
distribution APIs need a distribution name; this does not necessarily correlate with the package name the 'current' module is operating under.
Take the BeautifulSoup project, for example. The current version uses bs4
as the package name, but the distribution name on PyPI is beautifulsoup4
.
A distribution can contain more than one top-level package or module, too; the pkg_resources
module itself is part of the setuptools
distribution for example, together with the setuptools
package.
As such, you cannot generalize and use __file__
or __package__
and hope to always get the corresponding distribution.
If your distribution was installed as an egg (using easy_install
or zc.buildout
for example), then you can search for the .egg
extensions in the path, split the path at that location and use Distribution.from_filename()
to produce a Distribution
object from that; the filename contains all the information necessary. If your package was installed using pip
however, a separate .egg-info
file is created instead and you are back to square one (no distribution name locatable).
In the other direction, you can get all Distribution
objects for your environment with a pkg_resources.Environment()
instance; but you'll run into the same problems matching existing distributions to a given module or package, at least not with the public API. You could scan all distributions, the combination of the .location
attribute (for eggs) and perhaps the (private) Distribution._provider
attribute together with (entirely optional) presence of a installed-files.txt
metadata entry (which if is available, contains relative paths(!)) would let you scan your whole environment for a matching distribution, but this is entirely dependent on the internal implementation details of the various resource providers. And it could potentially be an expensive search, certainly for larger installations.
As such, your best option is to just use a hardcoded distribution name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With