在 bdist_wheel 中包含文件但不包含 sdist

Include file in bdist_wheel but not sdist

许多人抱怨 setuptools 在制作 sdist 时没有包括 package_data 中提到的内容。 (参见 here and here and here。)但是,这似乎在某些时候发生了变化,因此 package_data 中提到的项目不仅包含在 bdist_wheel 中,而且包含在 sdist 中还。我的问题是我想要旧的行为;我想要一个文件(即编译后的可执行文件)包含在 bdist_wheel 中,而不是 sdist 中。我现在该怎么做?

免责声明

尽管这在技术上是可行的,但请注意,当源 dist 不包含该文件而 wheel 包含该文件时,您最终会为相同的元数据安装相同的包,这是一种不良行为。在下面的示例中,

$ pip install spam --only-binary=spam  # force installation from wheel

将安装 file.txt:

$ pip show -f spam | grep file.txt
  spam/file.txt

$ pip install spam --no-binary=spam  # force installation from source dist

不会。这是引入新错误的明确来源,没有用户会感谢你的这个决定。


如果您确定这是您需要的:您可以排除 MANIFEST.in 中的文件。示例:

project
├── spam
│   ├── __init__.py
│   └── file.txt
├── MANIFEST.in
└── setup.py

MANIFEST.in

exclude spam/file.txt

setup.py

from setuptools import setup

setup(
    name='spam',
    version='0.1',
    packages=['spam'],
    package_data={'spam': ['file.txt']},
)

构建轮子:

$ python setup.py bdist_wheel >/dev/null 2>&1 && unzip -l dist/spam-*.whl | grep file.txt
    0  04-17-2019 21:25   spam/file.txt

建筑源分布:

$ python setup.py sdist --formats=zip >/dev/null 2>&1 && unzip -l dist/spam-*.zip | grep file.txt
<empty>