MattoTodd MattoTodd - 4 months ago 368
Python Question

PATH issue with pytest 'ImportError: No module named YadaYadaYada'

I used easy_install to install pytest on a mac and started writing tests for a project with a file structure likes so:

repo/
repo/app.py
repo/settings.py
repo/models.py
repo/tests/
repo/tests/test_app.py


run
py.test
while in the repo directory, everything behaves as you would expect

but when I try that same thing on either linux or windows (both have pytest 2.2.3 on them) it barks whenever it hits its first import of something from my application path. Say for instance
from app import some_def_in_app


Do I need to be editing my PATH to run py.test on these systems? Has Anyone experienced this?

Answer

yes, the source folder is not in python's path if you cd to the tests directory. you have 2 choices:

a. Add the path manually to the test files, something like:

import sys, os
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')

b. run the tests with the env var PYTHONPATH=../.