Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/    |--app.py    |--settings.py    |--models.py    |--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?

like image 586
MattoTodd Avatar asked Apr 20 '12 21:04

MattoTodd


People also ask

Where does pytest look for modules?

pytest looks for the conftest modules on test collection to gather custom hooks and fixtures, and in order to import the custom objects from them, pytest adds the parent directory of the conftest.py to the sys. path (in this case the repo directory).

Where do I put pytest files?

While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

What is SYS path in Python?

sys. path is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module. When a module(a module is a python file) is imported within a Python file, the interpreter first searches for the specified module among its built-in modules.


1 Answers

I'm not sure why py.test does not add the current directory in the PYTHONPATH itself, but here's a workaround (to be executed from the root of your repository):

python -m pytest tests/ 

It works because Python adds the current directory in the PYTHONPATH for you.

like image 168
Apteryx Avatar answered Sep 22 '22 16:09

Apteryx