title: Relative imports with unittest in Python
body: <p>I am trying to use Python unittest and relative imports, and I can't seem to figure it out.  I know there are a lot of related questions, but none of them have helped so far.  Sorry if this is repetitive, but I would really appreciate any help.  I was trying to use the syntax from PEP 328 <a href="http://www.python.org/dev/peps/pep-0328/" rel="noreferrer">http://www.python.org/dev/peps/pep-0328/</a> but I must have something wrong.  </p>
  
  <p>My directory structure is:</p>
  
  <pre><code>project/
      __init__.py
      main_program.py
      lib/
          __init__.py
          lib_a
          lib_b
      tests/
          __init__.py
          test_a
          test_b
  </code></pre>
  
  <p>I run my tests using:</p>
  
  <pre><code>python -m unittest test_module1 test_module2
  </code></pre>
  
  <p>test_a needs to import both lib/lib_a and main_program.  This is the code from test_a I am trying to use for the import:</p>
  
  <pre><code>from ..lib import lib_a as lib
  from ...project import main_program
  </code></pre>
  
  <p>both raise this error:</p>
  
  <pre><code>ValueError: Attempted relative import in non-package
  </code></pre>
  
  <p>All of my <strong>init</strong>.py files are currently empty.  </p>
  
  <p>Any specific advice would be greatly appreciated!!</p>
  
  <p>Edit:</p>
  
  <p>This may be the answer:  <a href="https://stackoverflow.com/questions/1342975/python-packages">Python Packages?</a>
  I'm still verifying if this will work.  </p>
  
  <p>Edit II:</p>
  
  <p>To clarify, at this point I have attempted to run my test file in 3 different ways:</p>
  
  <pre><code>project/tests $ python -m unittest test_a
  project/tests $ python -m test_a
  project/tests $ ./test_a
  </code></pre>
  
  <p>All three fail with the same error as above.  When I use the same three syntaxes but in the project directory, I get this error:</p>
  
  <pre><code>ValueError: Attempted relative import beyond toplevel package
  </code></pre>
  
  <p>Thanks again.  </p>