Jessada Thutkawkorapin Jessada Thutkawkorapin - 1 month ago 8
Python Question

Any python function to get "data_files" root directory?

This should be a very common question for developers who used "setup.py" to build installation packages and it should be asked before but I couldn't find the proper answer anywhere.

In setup.py

from distutils.core import setup
setup(
....,
....,
data_files=[('MyApp/CBV', ['myapp/data/CBV/training.cbv', 'myapp/data/CBV/test.cbv'])],
....,
....,
)


Result of sudo python setup.py install

running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
running install_data
creating /usr/local/MyApp/CBV
copying MyApp/data/CBV/training.cbv -> /usr/local/MyApp/CBV
copying MyApp/data/CBV/test.cbv -> /usr/local/MyApp/CBV
running install_egg_info
Removing /usr/local/lib/python2.7/dist-packages/MyApp-0.1.0.egg-info
Writing /usr/local/lib/python2.7/dist-packages/MyApp-0.1.0.egg-info


From the observation using the result above, "/usr/local" is the "data_files" root directory. Other than hardcoding, are there any Python functions that can give me this "data_files" root directory?

With suggestion from Martijn Pieters, here is my solution

In setup.py

from setuptools import setup
setup(
....,
....,
package=['myapp'],
package_data={'': ['data/CBV/*.cbv']},
....,
....,
)


Usage

import pkg_resources

my_data_file = pkg_resources.resource_filename(__name__, 'data/CBV/test.cbv')
print my_data_file

Answer

By default, when installing a package as root, the data path is based on sys.prefix plus your project name, I believe, so you can retrieve your files based on that.

You would be better off using the setuptools extension to distutils and use the pkg_resources module Resource API to load data files. It comes with setuptools for this very purpose.

You do want your data files included in the package for that to work best. See Including data files (in the setuptools documentation).

You can then load resource files with straight into a string with resource_string for example:

from pkg_resources import resource_string

foo_config = resource_string(__name__, 'foo.conf')
Comments