python - PATH issue with pytest 'ImportError: No module named YadaYadaYada'

ID : 10194

viewed : 22

Tags : pythonunit-testingpytestpython

Top 5 Answer for python - PATH issue with pytest 'ImportError: No module named YadaYadaYada'

vote vote


I'm not sure why py.test does not add the current directory in the PYTHONPATH itself, but here's a workaround (to be executed from the root of your repository):

python -m pytest tests/ 

It works because Python adds the current directory in the PYTHONPATH for you.

vote vote


conftest solution

The least invasive solution is adding an empty file named in the repo/ directory:

$ touch repo/ 

That's it. No need to write custom code for mangling the sys.path or remember to drag PYTHONPATH along, or placing into dirs where it doesn't belong (using python -m pytest as suggested in Apteryx's answer is a good solution though!).

The project directory afterwards:

repo ├── ├── ├── ├── └── tests      └── 


pytest looks for the conftest modules on test collection to gather custom hooks and fixtures, and in order to import the custom objects from them, pytest adds the parent directory of the to the sys.path (in this case the repo directory).

Other project structures

If you have other project structure, place the in the package root dir (the one that contains packages but is not a package itself, so does not contain an, for example:

repo ├── ├── spam │   ├── │   ├── │   └── ├── eggs │   ├── │   └── └── tests      ├──      └── 

src layout

Although this approach can be used with the src layout (place in the src dir):

repo ├── src │   ├── │   ├── spam │   │   ├── │   │   ├── │   │   └── │   └── eggs  │       ├── │       └── └── tests      ├──      └── 

beware that adding src to PYTHONPATH mitigates the meaning and benefits of the src layout! You will end up with testing the code from repository and not the installed package. If you need to do it, maybe you don't need the src dir at all.

Where to go from here

Of course, conftest modules are not just some files to help the source code discovery; it's where all the project-specific enhancements of the pytest framework and the customization of your test suite happen. pytest has a lot of information on conftest modules scattered throughout their docs; start with local per-directory plugins

Also, SO has an excellent question on conftest modules: In py.test, what is the use of files?

vote vote


I had the same problem. I fixed it by adding an empty file to my tests directory.

vote vote


Yes, the source folder is not in Python's path if you cd to the tests directory.

You have 2 choices:

  1. Add the path manually to the test files, something like this:

    import sys, os myPath = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, myPath + '/../') 
  2. Run the tests with the env var PYTHONPATH=../.

vote vote


Run pytest itself as a module with: python -m pytest tests

Top 3 video Explaining python - PATH issue with pytest 'ImportError: No module named YadaYadaYada'