Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test doesn't find module

Tags:

python

pytest

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

  • PATH issue with pytest 'ImportError: No module named YadaYadaYada'
  • Py.test No module named *

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?

like image 671
hildensia Avatar asked Mar 20 '15 10:03

hildensia


People also ask

Why is my module not found Python?

Here are a few reasons why a module may not be found: you do not have the module you tried importing installed on your computer. you spelled a module incorrectly (which still links back to the previous point, that the misspelled module is not installed)...for example, spelling numpy as numpys during import.

What is a pytest module?

PyTest is a testing framework that allows users to write test codes using Python programming language. It helps you to write simple and scalable test cases for databases, APIs, or UI. PyTest is mainly used for writing tests for APIs. It helps to write tests from simple unit tests to complex functional tests.

What is pytest Conftest py?

conftest.py is where you setup test configurations and store the testcases that are used by test functions. The configurations and the testcases are called fixture in pytest.


1 Answers

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
like image 156
hildensia Avatar answered Sep 18 '22 15:09

hildensia