Jessada Thutkawkorapin Jessada Thutkawkorapin - 1 year ago 106
Python Question

Any python function to get "data_files" root directory?

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


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

Result of sudo python 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


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


import pkg_resources

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

Answer Source

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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download