python - pkg_resources: get own distribution? -


i want find distribution of file. file should discovery own distribution.

i tried this, not work:

import os import pkg_resources dist in pkg_resources.find_distributions(os.path.dirname(__file__)):     print dist 

the file of above installed using pip install -e ....

i not find solution in docs:

https://pythonhosted.org/setuptools/pkg_resources.html#distribution-objects

the solution should not contain string of package. should generic.

the pkg_resources distribution apis need distribution name; not correlate package name 'current' module operating under.

take beautifulsoup project, example. current version uses bs4 package name, distribution name on pypi beautifulsoup4.

a distribution can contain more 1 top-level package or module, too; pkg_resources module part of setuptools distribution example, setuptools package.

as such, cannot generalize , use __file__ or __package__ , hope corresponding distribution.

if distribution installed egg (using easy_install or zc.buildout example), can search .egg extensions in path, split path @ location , use distribution.from_filename() produce distribution object that; filename contains information necessary. if package installed using pip however, separate .egg-info file created instead , square 1 (no distribution name locatable).

in other direction, can distribution objects environment pkg_resources.environment() instance; you'll run same problems matching existing distributions given module or package, @ least not public api. could scan distributions, combination of .location attribute (for eggs) , perhaps (private) distribution._provider attribute (entirely optional) presence of installed-files.txt metadata entry (which if available, contains relative paths(!)) let scan whole environment matching distribution, entirely dependent on internal implementation details of various resource providers. , potentially expensive search, larger installations.

as such, best option use hardcoded distribution name.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -