hildensia hildensia - 4 months ago 11
Python Question

py.test doesn't find module

This question is related to the following questions, but is not answered there:



I have a python module with the following tree structure:

mcts
|- setup.py
|- mcts
|- __init__.py
|- uct.py
|- toy_world_state.py
|- test
|- test_uct.py
|- test_toy_world_state.py


I create a virtualenv in some directory

$ mkdir virtual
$ virtualenv --system-site-packages virtual
$ source virtual/bin/activate


I then install my package:

$ cd /path/to/mcts
$ pip install -e .


Now I try to run the tests:

$ py.test mcts
================================================== test session starts ==================================================
platform linux -- Python 3.4.2 -- py-1.4.26 -- pytest-2.6.4
collected 0 items / 2 errors

======================================================== ERRORS =========================================================
__________________________________ ERROR collecting mcts/test/test_toy_world_state.py ___________________________________
mcts/test/test_toy_world_state.py:4: in <module>
from mcts.toy_world_state import *
E ImportError: No module named 'mcts'
________________________________________ ERROR collecting mcts/test/test_uct.py _________________________________________
mcts/test/test_uct.py:4: in <module>
from mcts.uct import *
E ImportError: No module named 'mcts'
================================================ 2 error in 0.02 seconds ===============================================


If I go to any path and try to import the module in
ipython
it works:

$ cd
$ ipython

In [1]: import mcts.uct

In [2]: mcts.uct?
Type: module
String form: <module 'mcts.uct' from '/home/johannes/src/mcts/mcts/uct.py'>
File: /home/johannes/src/mcts/mcts/uct.py
Docstring: <no docstring>


If I run pytest from within pycharm it works. (But I don't know what magic happens in pycharm...)

While
echo $PYTHONPATH
returns an empty string, the
sys.path
seems to be correct:

>>> import sys; print(sys.path)
['/home/johannes/src/mcts/virtualenvs/teste/lib/python3.4/site-packages',
'/home/johannes/src/mcts', '', '/usr/bin',
'/usr/lib/python3.4/site-packages/GitPython-0.3.2.RC1-py3.4.egg',
'/usr/lib/python34.zip', '/usr/lib/python3.4',
'/usr/lib/python3.4/plat-linux', '/usr/lib/python3.4/lib-dynload',
'/usr/lib/python3.4/site-packages',
'/usr/lib/python3.4/site-packages/IPython/extensions',
'/home/johannes/.ipython']


What do I have to do, to get the pytests running?

Answer

I fixed it myself. For some reason pytest did not got the virtualenv correct. Installing pytest in the virtualenv solved it

source virtualenvs/teste/bin/activate
pip install pytest
Comments