How do you add additional files to a wheel?

风流意气都作罢 提交于 2019-11-27 06:37:09

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.
    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.

  1. If you are building a source distribution (sdist) then you can use any method below.

  2. If you are building a wheel (bdist_wheel), then include_package_data and MANIFEST.in are ignored and you must use package_data and data_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).

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')),
    ],
)

I had config/ directory with JSON files in it, which I needed to add to the wheel package. So, I've added these lines to MANIFEST.in:

recursive-include config/ *.json

The following directive to setup.py:

setup(
 ...
 include_package_data=True,
)

And nothing worked. Until I've created an empty file called __init__.py inside config/ directory.

(Python 3.6.7, wheel 3.6.7, setuptools 39.0.1)

"A wheel is a ZIP-format archive ..." (http://wheel.readthedocs.org/en/latest)

So, treat the .whl file just like a .zip file. Add a file with:

  • Python ZipFile object
  • any archive/zip file utility, like 7-zip, Winzip, Ark, file-roller, etc.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!