Revision 2 as of 2004-04-08 03:33:20

Clear message

Problem

Distutils requires that you manually specify each package to be included in the distribution. For packages with large and deep sub-package hierarchies it can be a pain to keep this list in sync with the code, particularly as forgetting an entry is not noticable until a user happens to report that an entire sub-package is missing.

Solution

Use an automatic sub-package scanning mechanism to generate the package_dir and packages parameters for setup:

import os
def isPackage( filename ):
    return (
        os.path.isdir(filename) and
        os.path.isfile( os.path.join(filename,'__init__.py')
    )
def packagesFor( filename, basePackage="" ):
    """Find all packages in filename"""
    set = {}
    for item in os.listdir(filename):
        dir = os.path.join(filename, item)
        if isPackage( dir ):
            if basePackage:
                moduleName = basePackage+'.'+item
            else:
                moduleName = item
            set[ moduleName] = dir
            set.update( packagesFor( dir, moduleName))
    return set

Then call packagesFor to get the set of packages to be included (note that this call assumes that the packages are sub-directories of the directory where setup.py resides).

packages = packagesFor( "." )

Then use packages as the source within your call to setup:

setup (
    name = "pytable",
    package_dir = packages,
...
    packages = packages.keys(),
    **extraArguments
)

You can see a real-world usage example in the [http://cvs.sourceforge.net/viewcvs.py/pytable/table/setup.py?view=markup PyTable setup script]

Discussion

There should be some way to do this with distutils own machinery, I just don't know what it would be.


CategoryDistutilsCookbook

Unable to edit the page? See the FrontPage for instructions.