【问题标题】:Is it possible to exclude certain files when building a wheel with setup.py?使用 setup.py 构建轮子时是否可以排除某些文件?
【发布时间】:2015-01-10 17:25:37
【问题描述】:

我知道您可以使用以下方法排除某些包:

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

是否也可以排除单个 python 文件? 我正在构建一个二进制轮,并希望排除某些我使用自定义函数“cythonized”的源文件:

python cythonize bdist_wheel

目前,在使用自定义脚本构建轮子后,我删除了所有还具有 .so 库文件的 python 文件,我想使用 setup.py 来做到这一点。

【问题讨论】:

标签: python setuptools


【解决方案1】:

您可以将setuptools.find_packages()修订控制插件 一起使用,即setuptools-git

以下是setup.py 项目设置中的一些摘录,用于排除tests 目录:

from setuptools import setup, find_packages

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

上面使用的其他插件可用于 bzrdarcsmonotonemercurial 等.

提示:

不要忘记通过运行清理构建目录python setup.py clean --all bdist_wheel

【讨论】:

  • 但最初的问题是Is it possible to exclude single python files?,而您正在谈论排除目录。
  • @Palasaty,您能否在我的回答中添加类似的批评)))stackoverflow.com/a/45847842/1115187
  • python setup.py clean --all
  • bdist_wheel 的行为与 sdist 不同,这如此令人愤怒!这使我无法为我的纯 Python 库发布轮子,因为 bdist_wheel 会总是包含我设法从 sdist 中排除的内容。
  • @CharlieClark 你是什么意思?
【解决方案2】:

在 py-docs "How to include/exclude files to the package" 中有一篇模糊的 (IMO) 文章。 两个字:使用find_packagesMANIFEST.in的组合

要检查本地包中的内容(在发送到 PyPI 之前),请运行 python setup.py sdist,然后检查 ./dist 文件夹的内容(您的包中应该有 tarball)。

我的用例

忽略一个文件

MANIFEST.in 添加到包的根目录,并添加以下行:

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

此文件不会包含在分发包中。

在源附近进行测试

如果在你的项目中测试文件放在代码附近(换句话说,没有分隔目录tests),像这样:

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

您可以将此行添加到您的MANIFEST.in,而setuptools 将忽略测试文件:

global-exclude *_test.py

另见

【讨论】:

【解决方案3】:

如果你使用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',
        ...
    }
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2020-01-30
    • 2019-07-01
    • 2017-02-20
    • 1970-01-01
    • 2014-04-07
    • 2015-04-05
    相关资源
    最近更新 更多