title: Importing correctly with pytest
body: <p>I just got set up to use pytest with Python 2.6. It has worked well so far with the exception of handling "import" statements: I can't seem to get pytest to respond to imports in the same way that my program does.</p>
  
  <p>My directory structure is as follows:</p>
  
  <pre><code>src/
      main.py
      util.py
      test/
          test_util.py
      geom/
          vector.py
          region.py
          test/
              test_vector.py
              test_region.py
  </code></pre>
  
  <p>To run, I call <code>python main.py</code> from src/.</p>
  
  <p>In main.py, I import both vector and region with</p>
  
  <pre><code>from geom.region import Region
  from geom.vector import Vector
  </code></pre>
  
  <p>In vector.py, I import region with</p>
  
  <pre><code>from geom.region import Region
  </code></pre>
  
  <p>These all work fine when I run the code in a standard run. However, when I call "py.test" from src/, it consistently exits with import errors.</p>
  
  <hr>
  
  <h2>Some Problems and My Solution Attempts</h2>
  
  <p>My first problem was that, when running "test/test_foo.py", py.test could not "import foo.py" directly. I solved this by using the "imp" tool. In "test_util.py":</p>
  
  <pre><code>import imp
  util = imp.load_source("util", "util.py")
  </code></pre>
  
  <p>This works great for many files. It also seems to imply that when pytest is running "path/test/test_foo.py" to test "path/foo.py", it is based in the directory "path".</p>
  
  <p>However, this fails for "test_vector.py". Pytest can find and import the <code>vector</code> module, but it <strong>cannot</strong> locate any of <code>vector</code>'s imports. The following imports (from "vector.py") both fail when using pytest:</p>
  
  <pre><code>from geom.region import *
  from region import *
  </code></pre>
  
  <p>These both give errors of the form</p>
  
  <pre><code>ImportError: No module named [geom.region / region]
  </code></pre>
  
  <p>I don't know what to do next to solve this problem; my understanding of imports in Python is limited.</p>
  
  <p><strong>What is the proper way to handle imports when using pytest?</strong></p>
  
  <hr>
  
  <h2>Edit: Extremely Hacky Solution</h2>
  
  <p>In <code>vector.py</code>, I changed the import statement from</p>
  
  <pre><code>from geom.region import Region
  </code></pre>
  
  <p>to simply</p>
  
  <pre><code>from region import Region
  </code></pre>
  
  <p>This makes the import relative to the directory of "vector.py".</p>
  
  <p>Next, in "test/test_vector.py", I add the directory of "vector.py" to the path as follows:</p>
  
  <pre><code>import sys, os
  sys.path.append(os.path.realpath(os.path.dirname(__file__)+"/.."))
  </code></pre>
  
  <p>This enables Python to find "../region.py" from "geom/test/test_vector.py".</p>
  
  <p>This works, but it seems extremely problematic because I am adding a ton of new directories to the path. What I'm looking for is either</p>
  
  <p>1) An import strategy that is compatible with pytest, or</p>
  
  <p>2) An option in pytest that makes it compatible with my import strategy</p>
  
  <p>So I am leaving this question open for answers of these kinds.</p>