bearrito bearrito - 4 months ago 17
Python Question

Module Import Error running py.test with modules on Path

I'm unable to import my module for testing in the way that I'd like. I'm running all of this in a virtualenv on 2.7.2

I have a directory structure like

/api
/api
__init__.py
my_module.py
/tests
my_module_test.py


I have my PYTHONPATH set to /Path/api/. I CD into /Path/api and run the following

py.test tests/my_module_test.py


It does not work in the following case:


  1. When I have the following at the top of my_module_test.py
    from api.my_module import my_function



It does work in the following case:


  1. When I have the following at the top of my_module_test.py
    from my_module import my_function



Why am I not able to import my module as in case 1?

Answer

I use PYTHONPATH as

PYTHONPATH=`pwd` py.test tests/my_module_test.py
Comments