Python: Best way to add to sys.path relative to the current running script
<p>I have a directory full of scripts (let's say <code>project/bin</code>). I also have a library located in <code>project/lib</code> and want the scripts to automatically load it. This is what I normally use at the top of each script:</p>

<pre><code>#!/usr/bin/python
from os.path import dirname, realpath, sep, pardir
import sys
sys.path.append(dirname(realpath(__file__)) + sep + pardir + sep + "lib")

# ... now the real code
import mylib
</code></pre>

<p>This is kind of cumbersome, ugly, and has to be pasted at the beginning of every file. Is there a better way to do this?</p>

<p>Really what I'm hoping for is something as smooth as this:</p>

<pre><code>#!/usr/bin/python
import sys.path
from os.path import pardir, sep
sys.path.append_relative(pardir + sep + "lib")

import mylib
</code></pre>

<p>Or even better, something that wouldn't break when my editor (or someone else who has commit access) decides to reorder the imports as part of its clean-up process:</p>

<pre><code>#!/usr/bin/python --relpath_append ../lib
import mylib
</code></pre>

<p>That wouldn't port directly to non-posix platforms, but it would keep things clean.</p>
