简体   繁体   English

使用 setup.py 构建轮子时是否可以排除某些文件?

[英]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?是否也可以排除单个 python 文件? I am building a binary wheel and want to exclude certain source files which I "cythonized" with a custom function:我正在构建一个二进制轮,并希望排除某些我使用自定义函数“cythonized”的源文件:

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.目前,我在使用自定义脚本构建轮子后删除了所有也有 .so 库文件的 python 文件,我想用 setup.py 来做到这一点。

There is a vague (IMO) article in py-docs "How to include/exclude files to the package" . py-docs "How to include/exclude files to the package" 中有一篇含糊不清的 (IMO) 文章。 In two words : use combination of find_packages and MANIFEST.in两个字:结合使用find_packagesMANIFEST.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).要检查本地包中的内容(在发送到 PyPI 之前),运行python setup.py sdist ,然后检查./dist文件夹的内容(你的包应该有 tarball)。

My use-cases我的用例

Ignore one file忽略一个文件

Add MANIFEST.in to the root of your package, and add these lines:MANIFEST.in添加到包的根目录,并添加以下几行:

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:如果在你的项目中测试文件被放置在代码附近(换句话说,没有单独的目录tests ),像这样:

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:您可以将此行添加到您的MANIFEST.in ,并且setuptools将忽略测试文件:

global-exclude *_test.py

See also也可以看看

You can use setuptools.find_packages() along with a revision-control plugin ie setuptools-git .您可以将setuptools.find_packages()修订控制插件一起使用,即setuptools-git

Here is some extracts from a setup.py projects setup to exclude the tests directory:以下是setup.py项目设置的一些摘录,以排除tests目录:

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.其他插件如上面使用的插件可用于bzrdarcsmonotonemercurial等。

Tip:提示:

Don't forget to clean your build directory by running : python setup.py clean --all bdist_wheel不要忘记运行以下命令来清理构建目录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 .如果您使用include_package_data=True您还可以使用 setup() 函数中的exclude_package_data关键字。

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',
        ...
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dlib (setup.py) 循环的构建轮 - building wheel for dlib (setup.py) loop Pycares (Setup.Py) 错误的构建轮 - Building Wheel For Pycares (Setup.Py) Error Python - 为 lxml (setup.py) 构建轮...错误 - Python - Building wheel for lxml (setup.py) ... error dlib 的 OpenCV Python 构建轮(setup.py) - OpenCV Python Building wheel for dlib (setup.py) wxpython的构建轮(setup.py)......错误Windows - Building wheel for wxpython (setup.py) ... error Windows scipy (setup.py) 的构建轮:完成状态为“错误” - Building wheel for scipy (setup.py): finished with status 'error' 安装健身房的问题(为枕头建造轮子(setup.py)......错误) - Problem with installing gym ( Building wheel for Pillow (setup.py) … error) udatetime (setup.py) 错误/失败的构建轮 - WINDOWS - Building wheel for udatetime (setup.py) error / failed building wheel - WINDOWS Python setup.py - 运行 setup.py install 时不要构建轮子 - Python setup.py - dont build wheel when running setup.py install 构建 psycopg2-binary (setup.py) 的轮子……错误并运行 setup.py install for psycopg2-binary……错误 - Flask - Building wheel for psycopg2-binary (setup.py) … error and Running setup.py install for psycopg2-binary … error - Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM