30

I have a setup.py:

from setuptools import setup

setup(
      ...
      packages=['mypackage'],
      test_suite='mypackage.tests',
      ...
    )

python setup.py sdist creates a file that includes only the source modules from top-level mypackage and not mypackage.tests nor any other submodules.

What am I doing wrong?

Using python 2.7

user1561108
  • 2,666
  • 9
  • 44
  • 69

3 Answers3

31

Use the find_packages() function:

from setuptools import setup, find_packages

setup(
    # ...
    packages=find_packages(),
)

The function will search for python packages (directories with a __init__.py file) and return these as a properly formatted list. It'll start in the same dir as the setup.py script but can be given an explicit starting directory instead, as well as exclusion patterns if you need it to skip some things.

ryanjdillon
  • 17,658
  • 9
  • 85
  • 110
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • When I use this function, the `build/lib` never contains modules that are at the project root. If I use `packages=['.']`, running `python setup.py` shows a warning, but it does add modules at the project root into `build/lib`. My project root has both `__init__.py` and `setup.py` and `example-module.py`. – CMCDragonkai Mar 22 '18 at 00:59
  • 1
    @CMCDragonkai: you really should not make your project root a package, why do you have an `__init__.py` there to begin with? Your project root should only contain non-nested modules and packages, not *be* a package. – Martijn Pieters Mar 22 '18 at 07:48
  • Yes, I only realised that later. I thought that project root can be a package as well. – CMCDragonkai Mar 22 '18 at 08:00
3

For people using pure distutils instead of setuptools: you have to pass the list of all packages and subpackages (but not all submodules, they are detected) in the packages parameter.

merwok
  • 6,779
  • 1
  • 28
  • 42
3

Just include all your submodules in the packages list:

from setuptools import setup

setup(
      ...
      packages=['mypackage', 'mypackage.tests', 'mypackage.submodules'],
      ...
     )