PEP8 – import not at top of file with sys.path
<h1>Problem</h1>

<p>PEP8 has a rule about putting imports at the top of a file:</p>

<blockquote>
  <p>Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.</p>
</blockquote>

<p>However, in certain cases, I might want to do something like:</p>

<pre><code>import sys
sys.path.insert("..", 0)

import my_module
</code></pre>

<p>In this case, the <code>pep8</code> command line utility flags my code:</p>

<blockquote>
  <p>E402 module level import not at top of file</p>
</blockquote>

<p>What is the best way to achieve PEP8 compliance with <code>sys.path</code> modifications?</p>

<h1>Why</h1>

<p>I have this code because I'm following <a href="https://github.com/kennethreitz/samplemod">the project structure</a> given in <a href="http://docs.python-guide.org/en/latest/writing/structure/#structure-of-the-repository">The Hitchhiker's Guide to Python</a>.</p>

<p>That guide suggests that I have a <code>my_module</code> folder, separate from a <code>tests</code> folder, both of which are in the same directory. If I want to access <code>my_module</code> from <code>tests</code>, I think I need to add <code>..</code> to the <code>sys.path</code></p>