Get path to file within a package?
<p>I have a Python package which is organized like this:</p>

<pre><code>package
|- subpackage
|  |- code.py
|  |- window.ui
| ...
</code></pre>

<p>In <code>code.py</code> I want to access the file <code>window.ui</code> via</p>

<pre><code>PyQt4.uic.loadUi('window.ui', self)
</code></pre>

<p>This works well, if I just run <code>code.py</code> with <code>subpackage</code> as the working directory. But if I import the package from another working directory, this file cannot be found:</p>

<pre><code>IOError: [Errno 2] No such file or directory: 'window.ui'
</code></pre>

<p><strong>My question:</strong> How can I get the path name of the directory the file <code>code.py</code> is placed in, in order to create the absolute path name of <code>window.ui</code>. Or, how can I most efficiently access the file <code>window.ui</code>.</p>

<p>I tried <code>os.path.abspath('.')</code> from <a href="http://stackoverflow.com/questions/3758866/python-get-path-to-file-in-sister-directory">here</a>, but it only returns the absolute path of the current working directory.</p>

<p>Use absolute path of the file instead of relative path.</p>

<pre><code>abspath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "window.ui")

PyQt4.uic.loadUi(abspath, self)
</code></pre>

python import module