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 http://www.python.org/dev/peps/pep-0328/ but I must have something wrong.
My directory structure is:
project/
__init__.py
main_program.py
lib/
__init__.py
lib_a
lib_b
tests/
__init__.py
test_a
test_b
I run my tests using:
python -m unittest test_module1 test_module2
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:
from ..lib import lib_a as lib
from ...project import main_program
both raise this error:
ValueError: Attempted relative import in non-package
All of my init.py files are currently empty.
Any specific advice would be greatly appreciated!!
Edit:
This may be the answer: Python Packages? I'm still verifying if this will work.
Edit II:
To clarify, at this point I have attempted to run my test file in 3 different ways:
project/tests $ python -m unittest test_a
project/tests $ python -m test_a
project/tests $ ./test_a
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:
ValueError: Attempted relative import beyond toplevel package
Thanks again.
-m
's? And check on what relative imports are relative to? The pythonpath? The current directory? What? I just take guido's view and rarely, if ever, use them. If your tests are testing the lib module then put them into the lib with the routines they are testing. Only tests that are testing the project belong at the top level there.