Relative imports with unittest in Python -


i trying use python unittest , relative imports, , can't seem figure out. know there lot of related questions, none of them have helped far. sorry if repetitive, appreciate help. trying use syntax pep 328 http://www.python.org/dev/peps/pep-0328/ must have 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 tests using:

python -m unittest test_module1 test_module2 

test_a needs import both lib/lib_a , main_program. code test_a trying use import:

from ..lib import lib_a lib ...project import main_program 

both raise error:

valueerror: attempted relative import in non-package 

all of init.py files empty.

any specific advice appreciated!!

edit:

this may answer: python packages? i'm still verifying if work.

edit ii:

to clarify, @ point have attempted run test file in 3 different ways:

project/tests $ python -m unittest test_a project/tests $ python -m test_a project/tests $ ./test_a 

all 3 fail same error above. when use same 3 syntaxes in project directory, error:

valueerror: attempted relative import beyond toplevel package 

thanks again.

in experience easiest if project root not package, so:

project/   test.py   run.py   package/     __init__.py     main_program.py     lib/       __init__.py       lib_a       lib_b     tests/       __init__.py       test_a       test_b 

however, of python 3.2 , unittest module provides -t option, lets set top level directory, (from package/):

python -m unittest discover -t .. 

more details @ unittest docs.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -