Differences between revisions 2 and 3
Revision 2 as of 2004-04-08 03:33:20
Size: 1826
Editor: CPE0004e21253a5-CM012059940518
Comment:
Revision 3 as of 2007-10-25 10:12:25
Size: 1823
Editor: lmlo
Comment: Fix SF link
Deletions are marked like this. Additions are marked like this.
Line 49: Line 49:
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] You can see a real-world usage example in the [http://pytable.cvs.sourceforge.net/pytable/table/setup.py?view=markup PyTable setup script]

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://pytable.cvs.sourceforge.net/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

Distutils/Cookbook/AutoPackageDiscovery (last edited 2010-04-11 01:25:10 by 101)

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