简体   繁体   中英

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

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

conftest.py contains:

import pytest

import sys
print sys.path

from Foo.Foo import Foo

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

test_Foo.py contains:

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

If I run pytest . in the Test folder then I get an error that it can not find the module 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?

The thing is that it only works when I move conftest.py to the folder below.

I run pytest 3.2.2

The error says the conftest.py can not be loaded because of an ImportError . Try moving your import inside the foo fixture like this:

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.

pip install virtualenv
virtualenv venv
. ./venv/bin/activate

In order to install your local modules you need a setup.py file:

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 . . 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). So in requirements.txt put what you need for your Foo class, eg

json
flask==1.0.2

where have specified the version for flask but not json . Then in the requirements-test.txt file you put the following:

-r requirements.txt
pytest
-e .

The first line means that when you install requirements-test.txt you get all the requirements.txt as well. The -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).

To install the requirements-test.txt file you then run:

pip install -r requirements-test.txt

Now you can run your tests and it will find your Foo module. This is a good way to solve the problem in CI as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM