简体   繁体   中英

Is it possible to exclude certain files when building a wheel with setup.py?

I know you can exclude certain packages using:

packages = find_packages("src", exclude=["test"]),

Is it also possible to exclude single python files? I am building a binary wheel and want to exclude certain source files which I "cythonized" with a custom function:

python cythonize bdist_wheel

At the moment I remove all python files which also have a .so library file after building the wheel with a custom script and I would like to do that with setup.py.

There is a vague (IMO) article in py-docs "How to include/exclude files to the package" . In two words : use combination of find_packages and MANIFEST.in

To check, what is in the package locally (before sending to PyPI), run python setup.py sdist , and then check the content of ./dist folder (there should be tarball with your package).

My use-cases

Ignore one file

Add MANIFEST.in to the root of your package, and add these lines:

exclude .travis.yml
exclude appveyor.yml
exclude data/private/file.env

This files won't be included into distribution package.

Tests near sources

If in your project test files are placed near the code (in other words, there is no separated directory tests ), something like this:

package1
├── src
│   ├── __init__.py
│   ├── __init__test.py
│   ├── mymod.py
│   ├── mymod_test.py
│   ├── typeconv.py
│   └── typeconv_test.py
│
├── LICENSE
└── README.rst

You could add this lines to your MANIFEST.in , and setuptools will ignore test files:

global-exclude *_test.py

See also

You can use setuptools.find_packages() along with a revision-control plugin ie setuptools-git .

Here is some extracts from a setup.py projects setup to exclude the tests directory:

from setuptools import setup, find_packages

setup(
    name=...
    ...
    packages=find_packages(exclude=["tests"]),
    setup_requires=[
        'setuptools',
        'setuptools-git',
        'wheel',
    ]

Other plugins like the one used above are available for bzr , darcs , monotone , mercurial , etc.

Tip:

Don't forget to clean your build directory by running : python setup.py clean --all bdist_wheel

You can also use exclude_package_data keyword from setup() function if you use include_package_data=True .

from setuptools import setup

setup(
    name=...,
    ...,
    include_package_data=True,
    exclude_package_data={
        '': 'file_to_exclude_from_any_pkg.c',
        'pkg_name': 'file_to_exclude_from_pkg_name.c',
        ...
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM