gpt4 book ai didi

python - conftest.py导入错误: No module named Foo

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:22 24 4
gpt4 key购买 nike

我有以下目录结构

/home/ubuntu/test/
- Foo/
- Foo.py
- __init__.py
- Test/
- conftest.py
- __init__.py
- Foo/
- test_Foo.py
- __init__.py

Foo.py 包含

class Foo(object):
def __init__(self):
pass

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 包含:

class TestFoo():
def test___init__(self,foo):
assert True

如果我运行 pytest 。在 Test 文件夹中然后我得到一个错误,它找不到模块 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

conftest.py中打印出来的sys.path好像包含了/home/ubuntu/test路径所以应该能找到Foo.py吧?

问题是只有当我将 conftest.py 移动到下面的文件夹时它才有效。

我运行 pytest 3.2.2

最佳答案

错误表明 conftest.py 由于 ImportError 而无法加载。尝试像这样将您的导入移动到 foo fixture 中:

import pytest
import sys
print sys.path


@pytest.fixture(scope="session")
def foo():
from Foo.Foo import Foo
return Foo()

关于python - conftest.py导入错误: No module named Foo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203090/

24 4 0