How do you add additional files to a wheel?

前端 未结 6 601
北荒
北荒 2020-11-28 21:58

How do control what files are included in a wheel? It appears MANIFEST.in isn\'t used by python setup.py bdist_wheel.

UPDATE

6条回答
  •  情歌与酒
    2020-11-28 22:24

    include_package_data is the way to go, and it works for sdist and wheels.

    However you have to do it right, and it took me months to figure this out, so here is what I learned.

    The trick is essentially given in the name of the option include_PACKAGE_data: The data files need to be in a package subfolder

    If and only if

    • include_package_data is True
    • the data files are listed in MANIFEST.in (*see also my note at the end about setuptools_scm)
    • and the data files are under a package directory

    then the data files will be included.

    Working Example:

    Given the project has the following structure and files:

    |- MANIFEST.in
    |- setup.cfg
    |- setup.py
    |
    \---foo
        |- __init__.py
        |
        \---data
             - example.png
    
    

    And the following configuration:

    Manifest.in:

    recursive-include foo/data *
    

    setup.py

    import setuptools
    
    setuptools.setup()
    

    setup.cfg

    [metadata]
    name = wheel-data-files-example
    url = www.example.com
    maintainer = None
    maintainer_email = none@example.com
    
    [options]
    packages =
        foo
    include_package_data = True
    

    sdist packages and your wheels you build will contain the example.png datafile as well.

    (of course, instead of setup.cfg the config can also be directly specified in setup.py. But this is not relevant for the example.)

    Update: For src layout projects

    This should also work for projects that use a src layout, looking like this:

    |- MANIFEST.in
    |- setup.cfg
    |- setup.py
    |
    \---src
        |
        \---foo
            |- __init__.py
            |
            \---data
                 - example.png
    

    To make it work, tell setuptools about the src directory using package_dir:

    setup.cfg

    [metadata]
    name = wheel-data-files-example
    url = www.example.com
    maintainer = None
    maintainer_email = none@example.com
    
    [options]
    packages =
        foo
    include_package_data = True
    package_dir =
        =src
    

    And in the manifest adjust the path:

    Manifest.in:

    recursive-include src/foo/data *
    

    Note: No Manifest.in necessary if you use setuptools_scm

    If you happen to use setuptools and add the setuptools_scm plugin (on pypi), then you don't need to manage a Manifest.in file. Instead setuptools_scm will take care that all files that are tracked by git are added in the package.

    So for this case the rule for if or if not a file is added to the sdist/wheel is: If and only if

    • include_package_data is True
    • the file is tracked by git (or another setuptools_scm supported tool)
    • and the data files are under a package directory

    then the data files will be included.

提交回复
热议问题