Finding a file in a Python module distribution
<p>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/).</p>

<p>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 <code>__file__</code> variable in the module which accesses the database:</p>

<pre>
dbname = os.path.join(os.path.dirname(__file__), "database.dat")
</pre>

<p>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 "dbconfig.py" file that gets installed alongside the code that accesses the database.</p>

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

<pre><code>&gt;&gt;&gt; import pkg_resources
&gt;&gt;&gt; pkg_resources.resource_ filename(__name__, "foo.config")
'foo.config'
&gt;&gt;&gt; pkg_resources.resource_filename('tempfile', "foo.config")
'/usr/lib/python2.4/foo.config'
</code></pre>

<p>There's more discussion about using pkg_resources to get resources on the <a href="http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources">eggs</a> page and the <a href="http://peak.telecommunity.com/DevCenter/PkgResources">pkg_resources</a> page.</p>

<p>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.</p>
