Wanting to run a main program in a subdirectory of my hierarchy
<p>I'm working on an open-source Python project with a moderately large (and steadily increasing) number of demo programs.  </p>

<p>I moved the demo programs to a subdirectory named <code>demo/</code>, but then of course you cannot directly run them - if you do, Python takes the <code>demo/</code> subdirectory to be your root, and all your project imports fail.</p>

<p>My fix was to write a Demo.py program at the top level that lets you select which demo you want to run, but other developers and users of this project have (quite reasonably) complained about having to do this - they just want to run their demos directly.</p>

<p>I don't have a good solution for this.  The best idea I have is to put a code block like this at the top of each demo file:</p>

<pre><code>if __name__ == '__main__':
  import sys
  from os.path import dirname
  sys.path.append(dirname(dirname(__file__)))
</code></pre>

<p>which does seem to work, though it's inelegant.</p>

<ol>
<li>Is there a better solution?</li>
<li>If not, are there any obvious traps in this solution I should be aware of?</li>
</ol>
