[Distutils] Finding modules in an egg / distribution

Iwan Vosloo iwan at reahl.org
Tue Jul 2 19:30:35 CEST 2013

On 02/07/2013 17:08, PJ Eby wrote:
> If you are targeting at least Python 2.5, see: 
> http://docs.python.org/2/library/pkgutil.html#pkgutil.walk_packages 

We're targeting Python 2.7.

Trouble is that pkgutil.walk_packages needs a path to search from. 
Distribution.location is always your site-packages directory once a 
Distribution is installed, so walking that just gives ALL installed 
packages. If your Distribution contains some sort of main package that 
contains everything in it, you can use that package's .__path__, but 
then you'd need to discover what that package is.

Distributions could also contain more than one package next to each 
other, and top-level modules. (The __path__ of a top-level module is 
also simply the site-packages directory.)

There is a metadata file top_level.txt which one could use to get the 
names of top-level packages/modules in the Distribution. This can 
however contain a namespace package too - and you don't want all the 
packages inside the namespace package - just the bits inside the chosen 

- Iwan

More information about the Distutils-SIG mailing list