Dynamically updating the PYTHONPATH prevents .pyc update
<p>To allow myself to have a clear filestructure in my project i am using the following code snippet to dynamically add the project main folder to the PYTHONPATH and therefore assure that I can import files even from above a files location.</p>

<pre class="lang-py prettyprint-override"><code>import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "."))
</code></pre>

<p>Since I did this, when I start my main file, changes to the modules aren't recognized anymore until i manually delete any .pyc files. Thus I assume this for some reason prevented python from checking if the pyc files are up to date. Can I overcome this issue in any way?</p>

<p>Adding the path of an already imported module can get you into trouble if module names are no longer unique. Consider that you do <code>import foo</code>, which adds its parent package <code>bar</code> to <code>sys.path</code> - it's now possible to also do <code>import bar.foo</code>. Python will consider both to be different modules, which can mess up anything relying on module identity.</p>

<p>You should really consider why you need to do this hack in the first place. If you have an executable placed inside your package, you should not do</p>

<pre><code>cd bardir/bar
python foo
</code></pre>

<p>but instead call it as part of the package via</p>

<pre><code>cd bardir
python -m bar.foo
</code></pre>

python pythonpath pyc