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

|– share
|   |– data
|– src|
| `– yourpackage
|   |–

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:

#datadir = os.path.join('share','data')
#datafiles = [(datadir, [f for f in glob.glob(os.path.join(datadir, '*'))])]
# Based on Jeremy's comment,we can also us os.walk for recursion
datadir = os.path.join('share','data')
datafiles = [(d, [os.path.join(d,f) for f in files])
    for d, folders, files in os.walk(datadir)]
import metainfo # a file with relevant information
    name             = 'yourpackage',
    version          = metainfo.version,
    maintainer       = metainfo.maintainer,
    maintainer_email = metainfo.maintainer_email,
    author           = metainfo.authors,
    author_email     = metainfo.authors,
    description      = metainfo.description,
    keywords         = metainfo.keywords,
    long_description = metainfo.long_description,
    # package installation
    packages = find_packages('src'),
    package_dir  = package_dir,
    data_files = datafiles,

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

from os.path import join as pj
import yourpackage
def get_data(filename):
    packagedir = yourpackage.__path__[0]
    dirname = pj(os.path.dirname(packagedir), '..', 'share','data')
    fullname = os.path.join(dirname, filename)
    return fullname
Please follow and like us:
This entry was posted in Python and tagged , . Bookmark the permalink.

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

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

Leave a Reply

Your email address will not be published.