1

I'm new to Python and having some trouble understanding importing modules into Pytest. I solved the problem using Python 2.7 (through much searching and not much understanding). I've now switched to using Python 3.4 in a virtualenv.

My directory structure is (based this on http://pytest.org/latest/goodpractises.html):

-root
    -Extractor
        Extractor.py
    -test
        test_extractor.py

.

# test_extractor.py
import Extractor                    # I assume this is where I'm going wrong
import Extractor.Extractor          # Or this?

I've tried running py.test and py.test tests/test_extractor.py both from the root directory. The test runs in both cases, and gives the error:

E   ImportError: No module named 'Extractor'

Running py.test gives the following info:

platform darwin -- Python 3.4.2 -- py-1.4.30 -- pytest-2.7.2
rootdir: /my/user/path/root

Running py.test tests/test_extractor.py gives the following:

platform darwin -- Python 3.4.2 -- py-1.4.30 -- pytest-2.7.2
rootdir: my/user/path/root/test

I'd understand the latter example not working (cannot find the module as the /test directory is not the project root) but why does it not find it from the root dir?

This seems like it should be obvious but I'm struggling to understand exactly what is going on. Do I need __init__.py files (even in Python 3.4?) From http://pytest.org/latest/goodpractises.html:

Note

You can use Python3 namespace packages (PEP420) for your application but pytest will still perform test package name discovery based on the presence of init.py files. If you use one of the two recommended file system layouts above but leave away the init.py files from your directories it should just work on Python3.3 and above. From “inlined tests”, however, you will need to use absolute imports for getting at your application code.

Am I fundamentally misunderstanding what this means? Or writing the import statement wrong? Or calling the tests wrongly?

Community
  • 1
  • 1
user2672537
  • 343
  • 1
  • 4
  • 11
  • Have you actually *installed* `Extractor` in whatever environment you're running the tests in? You should certainly have an `__init__.py` in `/Extractor` if you don't already, and might want to consider writing a `setup.py` file in `/root` to let you easily install and test the package. Have a look at e.g. http://www.jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/ for getting a project set up properly. – jonrsharpe Jul 21 '15 at 10:24
  • What needs to be installed? Extractor is a class with nothing but an `__init__` method at the moment. – user2672537 Jul 21 '15 at 10:31
  • but presumably that won't always be he case! *"Installation"* can be as simple as making sure that the directory your package is in is on Python's path, so that you can `import` it. – jonrsharpe Jul 21 '15 at 10:35
  • If I'm running `py.test` from the `root` directory, won't it already be on my Python path? – user2672537 Jul 21 '15 at 10:39
  • The pytest root is just where it looks for tests; that won't affect the `import`. – jonrsharpe Jul 21 '15 at 12:20
  • This is the same problem as [Importing correctly with pytest](https://stackoverflow.com/questions/25827160/importing-correctly-with-pytest), and the same solutions apply. (You also have the same confusion about what rootdir does. :-) @jonrsharpe Pytest doesn't look for tests in the rootdir _per se_, though in many cases rootdir will happen to be set to the root of the tree where pytest is looking for tests.) – cjs May 04 '18 at 10:02

0 Answers0