[英]conftest.py ImportError: No module named Foo
I have the following directory structure 我有以下目录结构
/home/ubuntu/test/
- Foo/
- Foo.py
- __init__.py
- Test/
- conftest.py
- __init__.py
- Foo/
- test_Foo.py
- __init__.py
Foo.py contains Foo.py包含
class Foo(object):
def __init__(self):
pass
conftest.py contains: conftest.py包含:
import pytest
import sys
print sys.path
from Foo.Foo import Foo
@pytest.fixture(scope="session")
def foo():
return Foo()
test_Foo.py contains: test_Foo.py包含:
class TestFoo():
def test___init__(self,foo):
assert True
If I run pytest . 如果我运行pytest。 in the Test folder then I get an error that it can not find the module Foo:
在测试文件夹中,然后我得到一个错误,即找不到模块Foo:
Traceback (most recent call last):
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/_pytest/config.py", line 379, in _importconftest
mod = conftestpath.pyimport()
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/py/_path/local.py", line 662, in pyimport
__import__(modname)
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/_pytest/assertion/rewrite.py", line 212, in load_module
py.builtin.exec_(co, mod.__dict__)
File "/home/ubuntu/pythonVirtualEnv/local/lib/python2.7/site-packages/py/_builtin.py", line 221, in exec_
exec2(obj, globals, locals)
File "<string>", line 7, in exec2
File "/home/ubuntu/test/Test/conftest.py", line 6, in <module>
from Foo.Foo import Foo
ImportError: No module named Foo
ERROR: could not load /home/ubuntu/test/Test/conftest.py
The sys.path that is printed out in conftest.py seems to include the /home/ubuntu/test path so it should be able to find Foo.py, right? 在conftest.py中打印出的sys.path似乎包含/ home / ubuntu / test路径,因此它应该能够找到Foo.py,对吗?
The thing is that it only works when I move conftest.py to the folder below. 问题是,仅当我将conftest.py移至下面的文件夹时,它才起作用。
I run pytest 3.2.2 我运行pytest 3.2.2
The error says the conftest.py
can not be loaded because of an ImportError
. 该错误表明
conftest.py
由于ImportError
而无法加载。 Try moving your import inside the foo fixture like this: 尝试像这样在foo固定装置中移动导入:
import pytest
import sys
print sys.path
@pytest.fixture(scope="session")
def foo():
from Foo.Foo import Foo
return Foo()
What I would recommend you do is set up a virtual environment and install the Foo module in the virtual environment. 我建议您做的是设置虚拟环境,然后在虚拟环境中安装Foo模块。
pip install virtualenv
virtualenv venv
. ./venv/bin/activate
In order to install your local modules you need a setup.py
file: 为了安装本地模块,您需要一个
setup.py
文件:
from setuptools import setup
setup(
name='foo',
version='0.0.1',
author='My Name',
author_email='my.name@email.com',
packages=['Foo'],
)
Then you can install your Foo module within your virtual environment: pip install -e .
然后,您可以在虚拟环境中安装Foo模块:
pip install -e .
. 。 Then when you run your tests, they will pick up your module.
然后,当您运行测试时,他们将接您的模块。
For a more complete, long term way of doing this, consider using requirements files. 有关更完整,更长期的方法,请考虑使用需求文件。 I usually put the modules I need in two files named
requirements.txt
(for production) and requirements-test.txt
(for running tests). 我通常将我需要的模块放在两个文件中,分别名为
requirements.txt
(用于生产)和requirements-test.txt
(用于运行测试)。 So in requirements.txt
put what you need for your Foo class, eg 因此,在
requirements.txt
放入Foo类所需的内容,例如
json
flask==1.0.2
where have specified the version for flask
but not json
. 在哪里指定了
flask
的版本,但没有指定json
。 Then in the requirements-test.txt
file you put the following: 然后在
requirements-test.txt
文件中输入以下内容:
-r requirements.txt
pytest
-e .
The first line means that when you install requirements-test.txt
you get all the requirements.txt
as well. 第一行意味着,当您安装
requirements-test.txt
时,您也将获得所有requirements.txt
。 The -e .
-e .
is the magic that fixes the problem you've experienced here, ie it installs the Foo module (and any others you might have in this repo). 是解决您在此处遇到的问题的灵丹妙药,即它安装了Foo模块(以及此存储库中可能包含的任何其他模块)。
To install the requirements-test.txt
file you then run: 要安装
requirements-test.txt
文件,然后运行:
pip install -r requirements-test.txt
Now you can run your tests and it will find your Foo module. 现在您可以运行测试,它将找到您的Foo模块。 This is a good way to solve the problem in CI as well.
这也是解决CI问题的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.