How to embedded data files in python using setuptools

Within a Python package, it is useful to provide data files. These files are not python modules so you do not want to place same together with your module. Let us put them in a directory share/data. Suppose you have the following structure

.yourpackage
|– setup.py
|– share
|   |– data
|– src|
| `– yourpackage
|   |– __init__.py

Now, the setup file should include the directory share/data and its contents. There are different ways of doing it. We chose to save the share files within the distribution (not in a global share directory). The setup file should look like this:

Then you could have a script to automatically generate the proper path name to any file in ./share/data

This entry was posted in Python and tagged , . Bookmark the permalink.

7 Responses to How to embedded data files in python using setuptools

  1. Stu says:

    This seem to work with virtualenv + python 2.7 unfortunately.

  2. JeremyM says:

    I would recommend something like

    [ (d, [os.path.join(d, f) for f in files]) for d,folders,files in os.walk(datadir)]

    over glob for general cases as it’ll handle the recursion

    • Thomas Cokelaer says:

      Just rediscovered my post… and your comment on such a case (recursion). Indeed the os.walk and your recipe worked. I’ve edited the post. thanks

  3. Pingback: Building a binary into your Python package | Fahhem's Blog

  4. Craig says:

    data_files doesn’t preserve your relative directory structure, it will just dump every file into the .egg directory. 🙁

    • Hi,
      Seems to work for me under Linux/Python 2.7 but it is never easy to tune the setup as wanted so there may be slight differences on the system or within the setup.py that prevent
      the structure to be kept.
      Best,Thomas

  5. ChePazzo says:

    To install cleanly into a virtualenv, I do this:

    import os
    ROOT_DIR = os.getenv(‘VIRTUAL_ENV’)
    data_files=[
    (ROOT_DIR+’/etc’, [‘config/app.conf’]),
    ]

Leave a Reply

Your email address will not be published.