[英]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_packages
和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).要检查本地包中的内容(在发送到 PyPI 之前),运行
python setup.py sdist
,然后检查./dist
文件夹的内容(你的包应该有 tarball)。
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.此文件不会包含在分发包中。
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
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.其他插件如上面使用的插件可用于bzr 、 darcs 、 monotone 、 mercurial等。
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.