How do you add additional files to a wheel?
Have you tried using package_data
in your setup.py
? MANIFEST.in
seems targetted for python versions <= 2.6, I'm not sure if higher versions even look at it.
After exploring https://github.com/pypa/sampleproject, their MANIFEST.in
says:
# If using Python 2.6 or less, then have to include package data, even though
# it's already declared in setup.py
include sample/*.dat
which seems to imply this method is outdated. Meanwhile, in setup.py
they declare:
setup(
name='sample',
...
# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
include_package_data=True,
package_data={
'sample': ['package_data.dat'],
},
...
)
(I'm not sure why they chose a wildcard in MANIFEST.in
and a filename in setup.py
. They refer to the same file)
Which, along with being simpler, again seems to imply that the package_data
route is superior to the MANIFEST.in
method. Well, unless you have to support 2.6 that is, in which case my prayers go out to you.
Before you make any changes in MANIFEST.in
or setup.py
you must remove old output directories. Setuptools is caching some of the data and this can lead to unexpected results.
rm -rf build *.egg-info
If you don't do this, expect nothing to work correctly.
Now that is out of the way.
If you are building a source distribution (
sdist
) then you can use any method below.If you are building a wheel (
bdist_wheel
), theninclude_package_data
andMANIFEST.in
are ignored and you must usepackage_data
anddata_files
.
INCLUDE_PACKAGE_DATA
This is a good option, but bdist_wheel
does not honor it.
setup(
...
include_package_data=True
)
# MANIFEST.in
include package/data.json
DATA_FILES for non-package data
This is most flexible option because you can add any file from your repo to a sdist
or bdist_wheel
setup(
....
data_files=[
('output_dir',['conf/data.json']),
]
# For sdist, output_dir is ignored!
#
# For bdist_wheel, data.json from conf dir in root of your repo
# and stored at `output_dir/` inside of the sdist package.
)
PACKAGE_DATA for non-python files inside of the package
Similar to above, but for a bdist_wheel
let's you put your data files inside of the package. It is identical for sdist
but has more limitations than data_files
because files can only source from your package subdir.
setup(
...
package_data={'package':'data.json'},
# data.json must be inside of your actual package
)
You can use package_data
and data_files
in setup.py
to specify additional files, but they are ridiculously hard to get right (and buggy).
An alternative is to use MANIFEST.in
and add include_package_data=True
in setup()
of your setup.py
as indicated here.
With this directive, the MANIFEST.in
will be used to specify the files to include not only in source tarball/zip, but also in wheel and win32 installer. This also works with any python version (i tested on a project from py2.6 to py3.6).
UPDATE 2020: it seems the MANIFEST.in is not honored anymore by the wheel in Python 3, although it still is in the tar.gz, even if you set include_package_data=True
.
Here is how to fix that: you need to specify both include_package_data
and packages
.
If your Python module is inside a folder "pymod", here's the adequate setup:
setup( ...
include_package_data = True,
packages = ['pymod'],
)
If your python scripts are at the root, use:
setup( ...
include_package_data = True,
packages = ['.'],
)
Then you can open your .whl file with a zip archival software such as 7-zip to check that all the files you want are indeed inside.
You can specify extra files to install using the data_files directive. Is that what you're looking for? Here's a small example:
from setuptools import setup
from glob import glob
setup(
name='extra',
version='0.0.1',
py_modules=['extra'],
data_files=[
('images', glob('assets/*.png')),
],
)