py.test找不到模块

hil*_*sia 14 python pytest

这个问题与以下问题有关,但在那里没有回答:

我有一个python模块具有以下树结构:

mcts
|- setup.py
|- mcts
 |- __init__.py
 |- uct.py
 |- toy_world_state.py
 |- test
  |- test_uct.py
  |- test_toy_world_state.py
Run Code Online (Sandbox Code Playgroud)

我在某个目录中创建了virtualenv

$ mkdir virtual
$ virtualenv --system-site-packages virtual
$ source virtual/bin/activate
Run Code Online (Sandbox Code Playgroud)

然后我安装我的包:

$ cd /path/to/mcts
$ pip install -e .
Run Code Online (Sandbox Code Playgroud)

现在我尝试运行测试:

$ 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 ===============================================
Run Code Online (Sandbox Code Playgroud)

如果我去任何路径并尝试导入其中的模块ipython:

$ 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>
Run Code Online (Sandbox Code Playgroud)

如果我从pycharm中运行pytest它可以工作.(但我不知道pycharm中发生了什么魔法...)

虽然echo $PYTHONPATH返回一个空字符串,但sys.path似乎是正确的:

>>> 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']
Run Code Online (Sandbox Code Playgroud)

为了让pytests运行,我该怎么做?

hil*_*sia 15

我自己修好了.由于某种原因,pytest没有得到virtualenv正确.在virtualenv中安装pytest解决了它

source virtualenvs/teste/bin/activate
pip install pytest
Run Code Online (Sandbox Code Playgroud)


Jef*_*ald 5

我创建这个作为你的问题的答案和我自己的困惑.我希望它有所帮助.注意py.test命令行和tox.ini中的PYTHONPATH.

https://github.com/jeffmacdonald/pytest_test

具体来说:你必须告诉py.test和tox在哪里找到你所包含的模块.

使用py.test,你可以这样做:

PYTHONPATH=. py.test
Run Code Online (Sandbox Code Playgroud)

使用tox,将其添加到tox.ini中:

[testenv]
deps= -r{toxinidir}/requirements.txt
commands=py.test
setenv =
    PYTHONPATH = {toxinidir}
Run Code Online (Sandbox Code Playgroud)