36
votes

I'm trying to put a Python project into a tarball using setuptools. The problem is that setuptools doesn't appear to like the way that the source tree was originally setup (not by me, I must add). Everything that I actually want to distribute is in the top-level directory, rather than in a subdirectory like the setuptools docs talk about.

The tree has a directory, tests, that I don't want to have in the released package. However, using exclude_package_data doesn't seem to actually do any excluding, and I'd like to work out what I've done wrong.

My setup.py looks like this, in relevant part:

setup(
  name="project",
  packages=[''],
  include_package_data=True,
  exclude_package_data={'': ['tests']},
  test_suite='nose.collector',
)
7

7 Answers

27
votes

We use the following convention to exclude 'tests' from packages.

setup(
   name="project",
   packages=find_packages(exclude=("tests",)),
   include_package_data=True, 
   test_suite='nose.collector',
)

We also use MANIFEST.in to better control what include_package_data=True does.

18
votes

I have wasted several hours on the same problem, trying to exclude a module, I finally found that I had to remove the *.egg-info and build directories, that somehow retained the idea that the module had to be included.

16
votes

This is what I found in setuptools manual:

from setuptools import setup, find_packages
...
packages = find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),

In my case the following was enough to exclude top-level tests directory:

packages = find_packages(exclude=["tests.*", "tests"]),
12
votes

I have the following in my setup.py...

setup(name='pyfoo',
      version="1.0.2",
      description='Example for stack overflow',
      url='http://stackoverflow.com/',
      author='David Michael Pennington',
      author_email='mike /|at|\ pennington.net',
      license='GPL',
      platforms='any',
      keywords='Stack Overflow Example',
      entry_points = "",
      long_description=read('README.rst'),
      include_package_data=True,  # Checks MANIFEST.in for explicit rules
      #                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      packages=find_packages(),
      use_2to3=True,
      zip_safe=False,
      setup_requires=["setuptools_hg"],

I had a doc/ directory that was getting massive due to the number of images I had in it; this meant that the size of my sdist was growing over 500kB. Originally I had this in my MANIFEST.in...

include LICENSE CHANGES README.rst requirements.txt
recursive-exclude * __pycache__
recursive-exclude * *.pyc
recursive-exclude * *.pyo
recursive-exclude * *.orig

The only thing I had to do to exclude my doc directory was this line at the bottom of MANIFEST.in...

prune doc*

Using prune doc* suddenly removed all my doc/ directory from the sdist tarball. So, it looks like you just need to use this in a MANIFEST.in file...

prune tests*
7
votes

Ug, setuptools makes this really tricky :(

I don't know if this is what you want, but one project I work on uses a combination of two things:

from setuptools import setup, find_packages
...
packages = find_packages(exclude=['tests']),
data_files = os.walk(path_to_files),
1
votes

For similar purpose, my collegue wrote setuptools-dummy package: http://github.com/ella/setuptools-dummy/tree/master

Take a look at setuptools_dummy, modify excludes to your needs and it should work. If not, open an issue ;)

1
votes

An additional solution that worked in my case. Apparently: packages=setuptools.find_packages(exclude=["tests.*", "tests"]),

didn't work, but:

packages=setuptools.find_packages(exclude=["*tests.*", "*tests"]),

adding the star character at the beginning of the word did the trick.