Importing Python modules from a distant directory
<p>What's the shortest way to import a module from a distant, relative directory?</p>

<p>We've been using this code which isn't too bad except your current working directory <em>has</em> to be the same as the directory as this code's or the relative path breaks, which can be error prone and confusing to users.</p>

<pre><code>import sys
sys.path.append('../../../Path/To/Shared/Code')
</code></pre>

<p>This code (I think) fixes that problem but is a lot more to type.</p>

<pre><code>import os,sys
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), '../../../Path/To/Shared/Code')))
</code></pre>

<p>Is there a shorter way to append the absolute path? The brevity matters because this is going to have to be typed/appear in a lot of our files. (We can't factor it out because then it would be in the shared code and we couldn't get to it. Chicken &amp; egg, bootstrapping, etc.)</p>

<p>Plus it bothers me that we keep blindly appending to sys.path but that would be even more code. I sure wish something in the standard library could help with this.</p>

<p>This will typically appear in script files which are run from the command line. We're running Python 2.6.2.</p>

<p><strong>Edit:</strong>
The reason we're using relative paths is that we typically have multiple, independent copies of the codebase on our computers. It's important that each copy of the codebase use its own copy of the shared code. So any solution which supports only a single code base (e.g., 'Put it in site-packages.') won't work for us.</p>

<p>Any suggestions? Thank you!</p>
