Installing resource data files and finding them

Ben Finney ben+python at benfinney.id.au
Sat Sep 7 02:03:26 EDT 2013


Howdy all,

How do I package a Python application with its resource files separate
from the Python modules, such that the resource files can be discovered
by the Python code when it runs?


I am working on an existing Python application code base. The
application currently assumes that its resource files will be installed
to the same location as the python modules (using the Distutils
‘package_data’ option).

But this violates the Filesystem Hierarchy Standard used on GNU+Linux
<URL:http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html>. By that
standard, platform-independent resources belong in ‘/usr/share/…’, while
platform-dependent code libraries belong in ‘/usr/lib/…’.

Currently the upstream Python code uses ‘os.path.dirname(__file__)’ and
similar, to discover the location of the resource files. It thereby
assumes that the resource files are installed to the same location as
the Python code. The Distutils ‘package_data’ option appears to enforce
this, and I can't find any way for the files to be installed to a
different place using that option.

The Distutils ‘data_files’ option doesn't seem suitable for this
purpose. It allows the location to be specified (using ‘python
./setup.py --data-files=FOODIR’),, but there appears to be no way for
the application to know what location was specified later.

I will need to change both the build configuration and the application
code, so that instead it allows the resource files to be installed to an
appropriate location on the target system, and seeks the data files in
the correct location when run later.


Of course, the code should continue to work on systems that don't
conform to the FHS and allow resource data files to live alongside
platform code, so I can't just hard-code the new location.

I had hoped to use Python's standard library (Distutils? Distribute?
Something else?) to access the resources once installed to the correct
location. This needs to allow the choice of location to be made at
install-time, but discovered by the application at run-time.

Is there a way to do that, or is this a problem still unsolved in
Python?

-- 
 \        “Most people, I think, don't even know what a rootkit is, so |
  `\     why should they care about it?” —Thomas Hesse, Sony BMG, 2006 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list