title: Programmatically append to PYTHONPATH in VirtualEnv using .pth
body: <p>How can you use a Python script to programmatically append the current path to the current Virtualenv's PYTHONPATH, using either .pth or another method? </p>
  
  <p>This needs to work on both Windows and Linux.</p>
  
  <p>I'm trying to have a Python script <code>setup.py</code> in the project root path which when executed, adds the project root path to PYTHONPATH so other scripts in nested directories can import modules by relative import from the project root.</p>
  
  <pre><code>MyProjectRoot
  + setup.py
  + data 
  + source
      + foo
          + lib
              + qux
                  + A.py
      + bar
          + assets
              + B.py
      + baz
          + C.py
  </code></pre>
  
  <p>For example, in <code>C.py</code>, we can use</p>
  
  <pre><code>import source.foo.lib.qux.A
  import source.bar.assets.B
  </code></pre>
  
  <p>to avoid having to add a variant of the following to every file that wishes to use import relative to the project root:</p>
  
  <pre><code>basePath = os.path.abspath("../..")
  sys.path.append(basePath)
  </code></pre>