Python – Finding a file in a Python module distribution


I've written a Python package that includes a bsddb database of pre-computed values for one of the more time-consuming computations. For simplicity, my setup script installs the database file in the same directory as the code which accesses the database (on Unix, something like /usr/lib/python2.5/site-packages/mypackage/).

How do I store the final location of the database file so my code can access it? Right now, I'm using a hack based on the __file__ variable in the module which accesses the database:

dbname = os.path.join(os.path.dirname(__file__), "database.dat")

It works, but it seems… hackish. Is there a better way to do this? I'd like to have the setup script just grab the final installation location from the distutils module and stuff it into a "" file that gets installed alongside the code that accesses the database.

Best Solution

Try using pkg_resources, which is part of setuptools (and available on all of the pythons I have access to right now):

>>> import pkg_resources
>>> pkg_resources.resource_filename(__name__, "foo.config")
>>> pkg_resources.resource_filename('tempfile', "foo.config")

There's more discussion about using pkg_resources to get resources on the eggs page and the pkg_resources page.

Also note, where possible it's probably advisable to use pkg_resources.resource_stream or pkg_resources.resource_string because if the package is part of an egg, resource_filename will copy the file to a temporary directory.