hildensia hildensia - 1 year ago 102
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:

|- 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
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...)

returns an empty string, the
seems to be correct:

>>> import sys; print(sys.path)
'/home/johannes/src/mcts', '', '/usr/bin',
'/usr/lib/python34.zip', '/usr/lib/python3.4',
'/usr/lib/python3.4/plat-linux', '/usr/lib/python3.4/lib-dynload',

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download