Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 493
smarie Avatar asked Apr 17 '20 15:04

smarie


People also ask

How do I exclude tests from packages in Visual Studio?

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. Oddly enough, using exclude= () was not enough for me.

How to run all tests from a specific module in Python?

You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py (can be changed with the -p, --pattern flag): This will run all the test*.py modules inside the test package.

How do I run a single testcase in Python?

Also you can run a single TestCase or a single test method: You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py (can be changed with the -p, --pattern flag): This will run all the test*.py modules inside the test package.

Does Pip-installing mygrad put tests in the user’s path?

I included find_packages (exclude= ["tests"]) in setup.py, which seems to take care of things when installing mygrad from the tarball, however this does not seem to affect the wheel that gets built. That is, pip-installing this project puts tests in the user’s path despite my instructions to exclude it. Any advise would be greatly appreciated!


Video Answer


1 Answers

(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).

like image 113
smarie Avatar answered Oct 19 '22 20:10

smarie