8.2 Pure Python distribution (by package)

If you have more than a couple of modules to distribute, especially if they are in multiple packages, it's probably easier to specify whole packages rather than individual modules. This works even if your modules are not in a package; you can just tell the Distutils to process modules from the root package, and that works the same as any other package (except that you don't have to have an __init__.py file).

The setup script from the last example could also be written as

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      packages=[''],
      )
(The empty string stands for the root package.)

If those two files are moved into a subdirectory, but remain in the root package, e.g.:

<root>/
        setup.py
        src/      foo.py
                  bar.py
then you would still specify the root package, but you have to tell the Distutils where source files in the root package live:
from distutils.core import setup
setup(name='foobar',
      version='1.0',
      package_dir={'': 'src'},
      packages=[''],
      )

More typically, though, you will want to distribute multiple modules in the same package (or in sub-packages). For example, if the foo and bar modules belong in package foobar, one way to layout your source tree is

<root>/
        setup.py
        foobar/
                 __init__.py
                 foo.py
                 bar.py
This is in fact the default layout expected by the Distutils, and the one that requires the least work to describe in your setup script:
from distutils.core import setup
setup(name='foobar',
      version='1.0',
      packages=['foobar'],
      )

If you want to put modules in directories not named for their package, then you need to use the package_dir option again. For example, if the src directory holds modules in the foobar package:

<root>/
        setup.py
        src/
                 __init__.py
                 foo.py
                 bar.py
an appropriate setup script would be
from distutils.core import setup
setup(name='foobar',
      version='1.0',
      package_dir={'foobar': 'src'},
      packages=['foobar'],
      )

Or, you might put modules from your main package right in the distribution root:

<root>/
        setup.py
        __init__.py
        foo.py
        bar.py
in which case your setup script would be
from distutils.core import setup
setup(name='foobar',
      version='1.0',
      package_dir={'foobar': ''},
      packages=['foobar'],
      )
(The empty string also stands for the current directory.)

If you have sub-packages, they must be explicitly listed in packages, but any entries in package_dir automatically extend to sub-packages. (In other words, the Distutils does not scan your source tree, trying to figure out which directories correspond to Python packages by looking for __init__.py files.) Thus, if the default layout grows a sub-package:

<root>/
        setup.py
        foobar/
                 __init__.py
                 foo.py
                 bar.py
                 subfoo/
                           __init__.py
                           blah.py
then the corresponding setup script would be
from distutils.core import setup
setup(name='foobar',
      version='1.0',
      packages=['foobar', 'foobar.subfoo'],
      )
(Again, the empty string in package_dir stands for the current directory.)

See About this document... for information on suggesting changes.