[Distutils] A quick question on pkg_resources.find_distributions
Phillip J. Eby
pje at telecommunity.com
Wed Jul 13 18:15:21 CEST 2005
At 01:07 PM 7/13/2005 +0100, Paul Moore wrote:
>I have only one egg installed in my site-packages, namely setuptools.
>However, with the following script:
>
>import sys
>import pkg_resources
>
>def list_packages():
> dists = []
> for p in sys.path:
> for dist in pkg_resources.find_distributions(p):
> dists.append((p, dist))
>
> for p, dist in dists:
> print p, dist.name, dist.version
>
>if __name__ == '__main__':
> list_packages()
>
>I get the output:
>
>C:\Apps\Python24\lib\site-packages setuptools 0.5a12
>C:\Apps\Python24\Lib\site-packages\setuptools-0.5a12-py2.4.egg setuptools
>0.5a12
>
>How come I see setuptools twice?
You need to look at the .path attributes. What's happening is that
setuptools is on sys.path directly, but it's also in a *directory* on
sys.path. So, you're calling find_distributions() on site-packages, but
you're also calling it directly on the egg, so you get it twice.
In the future, I may separate these kinds of returns, such that there's a
way to ask find_distributions() to give you only exact matches, because
this will be needed for implementing the WorkingSet concept in the
pkg_resources refactoring.
>A complementary question - how can I detect when two Distribution
>instances refer to the "same" egg. Is comparing the path attribute
>sufficient?
You need os.path.normcase on Windows, and
os.path.normpath/os.path.realpath/etc. to be really sure. Sane path
comparison is unfortunately somewhat of a black art in Python.
More information about the Distutils-SIG
mailing list