Search code examples
pythonsetuptoolspython-wheel

Why can't I exclude `tests` directory from my python wheel using `exclude`?


Consider the following package structure:

enter image description here

With the following setup.py contents:

from setuptools import setup, find_packages

setup(
    name='dfl_client',
    packages=find_packages(exclude=['*tests*']),
    include_package_data=True,
    package_data={"": ['py.typed', '*.pyi']},
)

When I package it using python setup.py sdist bdist_wheel, the resulting wheel:

  • contains the py.typed file, which is good
  • contains the tests folder, while it should be excluded according to the find_packages doc.

I spent hours trying to understand why with no success. Especially because it seems to work for other projects !


Solution

  • (I spent so many time trying to understand this stupid issue that I answer my own question hoping that can save time to others facing the same problem)

    I finally found the culprit: it is a hidden interaction between setuptools_scm and the include_package_data=True flag.

    By itself, include_package_data=True does not make the tests directory be included in the wheel. However if setuptools_scm is installed and the folder is under version control (and the tests directory is in the list of git-managed files), then the exclude directive does not seem to be taken into account anymore.

    So the solution was simply to remove the include_package_data=True, that is actually not needed when package_data is present:

    from setuptools import setup, find_packages
    
    setup(
        name='dfl_client',
        packages=find_packages(exclude=['*tests*']),
        package_data={"": ['py.typed', '*.pyi']},
    )
    

    See setuptools doc on including files (that is actually very straightforward about include_package_data) and this related issue and workaround (the workaround seems to work for the wheel too, not only the sdist).