43

How can I include package_data for sdist without a MANIFEST.in file?

My setup.py looks like this:

import setuptools

setuptools.setup(
    name='foo',
    version='2015.3',
    license='commercial',
    packages=setuptools.find_packages(),

    package_data={'': ['foo/bar.txt']},
)

Versions:

user@host> python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> import setuptools
>>> setuptools.version.__version__
'3.6'

I just can't get foo/bar.txt included.

Or is this blog post still true? http://blog.codekills.net/2011/07/15/lies,-more-lies-and-python-packaging-documentation-on--package_data-/

Over the last hour, though, I've learned that these statements are somewhere between “dangerously misleading” and “damn lies”. This is because the primary type of Python package is a source package, and the canonical method for creating a source package is by using setup.py sdist. However, the data specified in package_data are not included in source distributions — they are only included in binary (setup.py bdist) distributions and installs (setup.py install).

How can I include package_data for sdist without a MANIFEST.in file?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
guettli
  • 25,042
  • 81
  • 346
  • 663

2 Answers2

71

TL;DR: The keys in the package_data dictionaries are packages; the values are lists of globs. '' is not a valid name for any Python package.

If you want to have bar.txt be installed next to the __init__.py of package foo, use

 package_data={'foo': ['bar.txt']}

I have the layout:

foo/
        __init__.py
        bar.txt
setup.py

Now, if foo is a package like above, do:

import setuptools

setuptools.setup(
    name='foo',
    version='2015.3',
    license='commercial',
    packages=setuptools.find_packages(),
    package_data={'foo': ['bar.txt']},
)

And after python setup.py sdist, I check the contents of dist/foo-2015.3.tar.gz

% tar tfz dist/foo-2015.3.tar.gz
...
foo-2015.3/foo/bar.txt
...

However, if I run your setup.py with package_data={'': ['foo/bar.txt']}, I can concur that the foo/bar.txt will not be added to the source distribution, except if the foo-2015.3.egg-info/SOURCES.txt already has the line for foo/bar.txt - in that case the file will pop up in the source distribution too

No manifest was used; the setuptools version was 3.6 (I deliberately installed the same, old version that you were using):

>>> import setuptools
>>> setuptools.__version__
'3.6'

The behaviour above also works in standard distutils: 2.6 Installing package data of the "legacy" distutils documentation; with a comment for 2.7, 3.1:

Changed in version [2.7, 3.1]: All the files that match package_data will be added to the MANIFEST file if no template is provided.

  • 2
    Thanks for the detailed answer. Unfortunately, I just followed these instructions verbatim and `bar.txt` failed to appear in `foo-2015.3.tar.gz`. You mention deliberately using an old version of setuptools, but you don't say why. Could that explain why it's not working? – ron rothman Aug 05 '15 at 03:31
  • 3
    This answer is not entirely correct. Empty string `''` is a special key that applies to all packages as described in [here](https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files): Notice that if you list patterns in package_data under the empty string, these patterns are used to find files in every package, even ones that also have their own patterns listed. – immerrr Jul 02 '18 at 19:04
21

I had the same issue and fixed it be removing :

include_package_data=True
John P
  • 319
  • 2
  • 2
  • This fixed it for me as well. – Gabriel Romon Jul 13 '18 at 13:37
  • 13
    Can somebody explain why? – QuantumLicht Oct 03 '18 at 18:48
  • The top entry here explains this: https://setuptools.readthedocs.io/en/latest/setuptools.html#new-and-changed-setup-keywords – Adam Erickson Oct 22 '19 at 21:38
  • 7
    @AdamErickson no it does not? – MappaM Dec 03 '20 at 12:18
  • See the warning in https://jwodder.github.io/kbits/posts/pypkg-data/#including-package-data-via-setup-py-setup-cfg: https://setuptools.pypa.io/en/latest/userguide/quickstart.html?highlight=include_package_data#including-data-files says “The data files must be specified via the distutils’ MANIFEST.in file.”, with not enough emphasis on **must be**. – Anaphory Nov 05 '21 at 09:46