15
votes

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?

3
I can't see a module toy_world_state within your file structure which may explain why from mcts.toy_world_state import * throws an error.NDevox
sorry, that was a typo in my question. I change that.hildensia
I wonder if you add the project to your $PYTHONPATH this might work and might be exactly what PyCharm is configured to do.adarsh
shouldn't do pip install -e . do that? (resp. add the package to the site-package directory of the virtualenv -- which should be in $PYTHONPATH)hildensia
Btw. if I do PYTHONPATH=. py.test mcts it works. If I do PYTHONPATH=./virtualenvs/teste/lib/python3.4/site-packages/ py-test mcts it does not. I'm totally confused.hildensia

3 Answers

15
votes

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
5
votes

I created this as an answer to your question and my own confusion. I hope it helps. Pay attention to PYTHONPATH in both the py.test command line and in the tox.ini.

https://github.com/jeffmacdonald/pytest_test

Specifically: You have to tell py.test and tox where to find the modules you are including.

With py.test you can do this:

PYTHONPATH=. py.test

And with tox, add this to your tox.ini:

[testenv]
deps= -r{toxinidir}/requirements.txt
commands=py.test
setenv =
    PYTHONPATH = {toxinidir}
-3
votes

Your error message says:

ImportError: No module named 'mcts'

Looking at the directory structure you provided, that error is telling you that you don't have an 'mcts' module/package. To fix it, you need to add an "__init__.py" file to the 'mcts' directory. Afterwards, you should be able to run 'py.test' without other arguments/settings.