101

I want to include the ./static/data.txt to setuptools, here is my code:

# setup.py
import os,glob
from setuptools import setup,find_packages

setup(
    name = "PotatoProject",
    version = "0.1.1",
    author = "Master Splinter",
    author_email = "splinter@initech.com",
    description = ("The potatoproject!"),
    url = 'http://www.google.com',
    license = "BSD",

    # adding packages
    packages=find_packages('src'),
    package_dir = {'':'src'},

    # trying to add files...
    include_package_data = True,
    package_data = {
        '': ['*.txt'],
        '': ['static/*.txt'],
        'static': ['*.txt'],
    },

    scripts=['src/startPotato'],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
    ],
)

The file system:

.
├── setup.py
└── src
    ├── distutils_setup.py
    ├── Potato
    │   ├── __init__.py
    │   ├── potatoData.txt
    │   └── printer.py
    ├── startPotato
    ├── static
    │   └── data.txt
    └── Tomato
        ├── big.py
        └── __init__.py

the output when running: python setup.py sdist

running sdist
running egg_info
creating src/PotatoProject.egg-info
writing src/PotatoProject.egg-info/PKG-INFO
writing top-level names to src/PotatoProject.egg-info/top_level.txt
writing dependency_links to src/PotatoProject.egg-info/dependency_links.txt
writing manifest file 'src/PotatoProject.egg-info/SOURCES.txt'
reading manifest file 'src/PotatoProject.egg-info/SOURCES.txt'
writing manifest file 'src/PotatoProject.egg-info/SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.txt

creating PotatoProject-0.1.1
creating PotatoProject-0.1.1/src
creating PotatoProject-0.1.1/src/Potato
creating PotatoProject-0.1.1/src/PotatoProject.egg-info
creating PotatoProject-0.1.1/src/Tomato
making hard links in PotatoProject-0.1.1...
hard linking setup.py -> PotatoProject-0.1.1
hard linking src/startPotato -> PotatoProject-0.1.1/src
hard linking src/Potato/__init__.py -> PotatoProject-0.1.1/src/Potato
hard linking src/Potato/printer.py -> PotatoProject-0.1.1/src/Potato
hard linking src/PotatoProject.egg-info/PKG-INFO -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/PotatoProject.egg-info/SOURCES.txt -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/PotatoProject.egg-info/dependency_links.txt -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/PotatoProject.egg-info/top_level.txt -> PotatoProject-0.1.1/src/PotatoProject.egg-info
hard linking src/Tomato/__init__.py -> PotatoProject-0.1.1/src/Tomato
hard linking src/Tomato/big.py -> PotatoProject-0.1.1/src/Tomato
Writing PotatoProject-0.1.1/setup.cfg
creating dist
Creating tar archive
removing 'PotatoProject-0.1.1' (and everything under it)

and no txt added! No static/data.txt nor Potato/potatoData.txt...

What am I missing?!

vvvvv
  • 25,404
  • 19
  • 49
  • 81
joaoricardo000
  • 4,764
  • 4
  • 25
  • 36

5 Answers5

160

As pointed out in the comments, there are 2 ways to add the static files:

1 - include_package_data=True + MANIFEST.in

A MANIFEST.in file in the same directory of setup.py that looks like this:

include src/static/*
include src/Potato/*.txt

With include_package_data = True in setup.py.

2 - package_data in setup.py

package_data = {
    'static': ['*'],
    'Potato': ['*.txt']
}

Specify the files inside the setup.py.


Do not use both include_package_data and package_data in setup.py.

include_package_data will nullify the package_data information.

Official docs:
https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html

joaoricardo000
  • 4,764
  • 4
  • 25
  • 36
  • 3
    Works! Apparently, MANIFEST.in picks out the which files to include *in addition* to what setup.py / distutils is smart enough to include by default. – chbrown Jan 18 '14 at 13:55
  • 14
    Doing this plus adding the `include_package_data = True` above worked fro me. Both had to be done: The manifest file and the setup.py directive. – djhaskin987 Jan 14 '15 at 00:03
  • I just add "include *.txt". Works great thnx – Priyeshj Nov 23 '15 at 23:27
  • 8
    It should be noted that anytime you specify `include_package_data=True`, you basically nullify the effect of `package_data`, and that's why setuptools expect you to include files in your `MANIFEST.in`. Use one of them (`package_data` vs `include_package_data=True` + `MANIFEST.in`) , not both. – konoufo Aug 10 '18 at 16:25
  • Is it possible to write to the static directory during runtime? – Hairy Aug 08 '19 at 00:52
  • 1
    Updated link: https://setuptools.readthedocs.io/en/latest/userguide/datafiles.html – rudolfbyker May 05 '21 at 08:38
  • 3
    For those of you using the `setup.cfg` instead of `setup.py`, just add the `include_package_data = True` in the [options] section to have the build include anything listed in MANIFEST.in. – Brent Sep 11 '21 at 17:57
  • Please mention that you should use Manifest.in since its a better option https://stackoverflow.com/questions/7522250/how-to-include-package-data-with-setuptools-distutils – Alexis Sep 20 '21 at 21:33
31

Include all files recursively:

recursive-include project_name/templates *
recursive-include project_name/static *

where project_name is a folder in the same line where you have setup.py file.

Anton Danilchenko
  • 2,318
  • 1
  • 25
  • 24
11

According to the docs, there are three ways to include package data files. You have two packages: Potato and Tomato. The static directory is not in either of those packages, so that is why your package_data dictionary in setup.py was not working. The manifest option requires that include_package_data is set to True in setup.py. Access non-package data files can be done the way found here.

emispowder
  • 1,787
  • 19
  • 21
11

Use following

packages = ['.','templates','static','docs'],

package_data={'templates':['*'],'static':['*'],'docs':['*'],},
Dadaso Zanzane
  • 6,039
  • 1
  • 25
  • 25
1

Given a project structure like this:

.
├── setup.py
└── your_package
    ├── __init__.py
    ├── (code etc)
    └── static
        ├── a.txt
        ├── b.txt
        └── docs
            ├── a.rst
            └── b.rst

To recursively include all files under static, add the following in your setup.py (you do not need to add any __init__.py files inside of the static folder):

setup(
    ...
    package_data={"your_package": ["static/**/*"]},
)

The recursive glob **/* requires setuptools>=62.3.0: https://stackoverflow.com/a/55432314/2111778 https://stackoverflow.com/a/64789489/2111778

xjcl
  • 12,848
  • 6
  • 67
  • 89