[Distutils] pkg_resources.find_distributions("./")

Phillip J. Eby pje at telecommunity.com
Sat Feb 11 17:38:03 CET 2006

At 10:33 AM 2/11/2006 +0100, Elvelind Grandin wrote:
>On 2/11/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > At 11:14 PM 2/10/2006 +0100, Elvelind Grandin wrote:
> > >On 2/10/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > > > There is no reliable way to tell what distribution you are in, if 
> all you
> > > > have is the directory name.  If you have a *module* name, however, 
> you can
> > > > use get_provider(modulename).get_metadata('PKG-INFO') to retrieve the
> > > > package's PKG-INFO text, which you can then use to find the 
> distribution
> > > > name and version.
> > > >
> > >
> > >The problem is that the package might not be installed. And
> > >get_provides needs it to be right?
> >
> > The module just needs to be importable.
> >
> >
>When I run get_provider("xx") it just seems to return an emtpy
>DefaultProvider instance. there is no egg_name and no metadata.  but I
>can import the package.

Argh.  I forgot about .egg-info distributions; they don't include this 
data, and if there's more than one of them installed in the same directory 
(e.g. site-packages) there is currently no way to tell *which* distribution 
the module is actually from.  I'd have to start including some kind of 
installation manifest in the egg-info and search all of them.  This is 
another fallout from adding system package support that needs to get fixed.

For now, I'd do something like:

     if '.' in modulename:
         top = modulename.split('.')[0]
         top = modulename
     candidates = []
     for dist in working_set:
         if not module.__file__.startswith(dist.location):
         if top not in list(dist.get_metadata_lines('top_level.txt')):

This should give you a list of candidate distributions.

More information about the Distutils-SIG mailing list