ModuleNotFoundError with pytest


I want my tests folder separate to my application code. My project structure is like so


from moduleone import ModuleOne

class MyProject(object)

from myproject.myproject import MyProject
import pytest


I use myproject.myproject since I use the command

python -m pytest

from the project root directory ./myproject/

However, then the imports within those modules fail with

E ModuleNotFoundError: No module named ‘moduleone’

I am running Python 3.7 and have read that since 3.3, empty __init__ files are no longer needed which means my project becomes an implicit namespace package

However, I have tried adding an file in myproject/myproject/ and also tried adding a file in myproject/ but neither works

I have read answers that say to mess with the paths and then upvoted comments in other questions saying not to.

What is the correct way and what am I missing?


Possibly related, I used a requirements.txt to install pytest using pip. Could this be related? And if so, what is the correct way to install pytest in this case?


One of the paths in sys.path is /usr/src/app/ which is a docker volume lined to /my/local/path/myproject/.

Should the volume be /my/local/path/myproject/myproject/ instead?

Asked By: myol



Be sure to include . dot in the $PYTHONPATH env var.

Use $ python -m site, or this code fragment to debug such issues:

import pprint
import sys

Your question managed to use myproject at three different levels. At least during debugging you might want to use three distinct names, to reduce possible confusion.

Answered By: J_H

So it seems that the sys.path has to include the application directory rather than the project root folder containing the application directory and test directory.

So in my case /my/local/path/myproject/myproject/ had to be in sys.path rather than /my/local/path/myproject/.

Then I could run pytest in /my/local/path/myproject/ (didn’t need python -m pytest). This meant that the modules within /myproject/myproject/ could find each other and the tests as well without any namespace nesting.

So my tests looked like

 from moduleone import ModuleOne
 import pytest

 def test_fun():
     assert ModuleOne.example_func() == True

That said, there seem to be many gotchas, so I have no idea if this is correct..

Answered By: myol

I ran into this issue as well and am using poetry for dependency management and direnv for my project specific environment variables. Please note, I am relatively new to Python so I don’t know if this is the correct fix.

Here is my entire .envrc file:

layout_poetry() {
  if [[ ! -f pyproject.toml ]]; then
    log_error 'No pyproject.toml found.  Use `poetry new` or `poetry init` to create one first.'
    exit 2

  local VENV=$(poetry env list --full-path | cut -d' ' -f1)
  if [[ -z $VENV || ! -d $VENV/bin ]]; then
    log_error 'No created poetry virtual environment found.  Use `poetry install` to create one first.'
    exit 2
  export VIRTUAL_ENV=$(echo "$VENV" | rev | cut -d'/' -f2- | rev)
  export POETRY_ACTIVE=1
  PATH_add "$VENV"

layout poetry
export PYTHONPATH="$PWD/project_name"

I don’t know if I need to layout poetry because it is supposed to be creating virtual environments for us already but this is what I coworker recommended so I went with it. Layout poetry also didn’t work without that function and it didn’t like when I added it to my zshenv so I added it here.

For this specific question, the last line is the money maker.

Answered By: Sam

Kept everything same and just added a blank test file at the root folder .. Solved

Here are the findings, this problem really bugged me for a while.
My folder structure was

    - static/ 
       - home.html  
    - functional 
    - unit  

and pytest would complain with the ModuleNotFoundError.

I introduced a mock test file at the same level as mathsapp and tests directory. The file contained nothing. Now pytest does not complain.

Result without the file

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:mak2006workspacegithubpython-rest-app-cont
collected 1 item / 1 error

=================================== ERRORS ====================================
_______________ ERROR collecting tests/functional/ ________________
ImportError while importing test module ''.
Hint: make sure your test modules/packages have valid Python names.
Traceback: in <module>
    from mathapp.service import sum
E   ModuleNotFoundError: No module named 'mathapp'
=========================== short test summary info ===========================
ERROR tests/functional/
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.24s ===============================

Results with the file

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:mak2006workspacegithubpython-rest-app-cont
collected 2 items .                                          [ 50%] .                                                [100%]

============================== 2 passed in 0.11s ==============================
Answered By: Mak2006

Solution: use the PYTHONPATH env. var


As mentioned by @J_H, you need to explicitly add the root directory of your project, since pytest only adds to sys.path directories where test files are (which is why @Mak2006’s answer worked.)

Good practice: use a Makefile or some other automation tool

If you do not want to type that long command all the time, one option is to create a Makefile in your project’s root dir with, e.g., the following:

.PHONY: test
    PYTHONPATH=. pytest

Which allows you to simply run:

make test

Another common alternative is to use some standard testing tool, such as tox.

Not sure if this solution was specific to my problem, but I simply add to my tests folder and that solved the problem.

Answered By: Erfan

In my case I added a to my test directory with this inside it:

import sys

My app code is at the same level as my test directory.

Answered By: Stagg

Using poetry and pytest 5.4.3, I had the following structure (some folders / files have been removed for clarity):

project structure

├── my_app
│   ├──
│   ├──
│   ├──
│   └──
├── poetry.lock
├── pyproject.toml
├── README.rst
└── tests
    └── utilities


pytest_plugins = [

which generated a module not found error for the fixture:

ImportError: Error importing plugin "utilities.db_postgresql_inmemory": No module named 'utilities'

None of the other answers have worked for me, as I have tried to add:

[me@linux ~/code/my_app]touch tests/utilities/
[me@linux ~/code/my_app]touch ./

I could make the import from work by REMOVING both files:

[me@linux ~/code/my_app]rm tests/utilities/ tests/
Answered By: Lionel Hamayon

In my case it is because I installed pytest on the system level but not in my virtual environment.

You can test this by python -m pytest. If you see ModuleNotFoundError: No module named 'pytest' then your pytest is at the system level.

Install pytest when the virtual environment is activated will fix this.

Answered By: Qin Heyang


See this answer:

Answered By: mike rodent

I was facing the issue which i resolved by

  • Installing pytest at the root of my project using pip install pytest
  • Adding blank in the sibling of my which i wanted to execute.
Answered By: Hamza usman ghani

In my case, I’ve created a separate app for testing with both and at the same level. After deleting error was gone.

Credit goes to qci-amos

Answered By: Taras Mykhalchuk

I have resolved it by adding export PYTHONPATH="your root dir/src"

export PYTHONPATH="/builds/project/src"

poetry run pytest …..

Answered By: user18103107

The simplest solution I found was to manually add my target module to syspath. Lets say you have a structure like this:

- src
  -- utils
  -- ...
- tests

This makes my test folder a sibling to my module’s src folder. If I start putting test_* files that need to import some of the module’s code, I can simply:

import src.utils.calculator

And this would be fine until I try to import a file that imports another file from the module. The solution is simple: add a to your tests folder, and put this line inside:

import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))

And just modify the last part relative to your module location and folder name

Answered By: stann1

I suggest you have a code structure like this:


And the content of file is:

pytest_plugins = ['helpers']

Run pytest again.

Answered By: Bu Ban Tan

For me, when I was checking my project structure I found parent directory and sub directory having same names. When I changed the directory name, I got it working. So,

# Did not work
- same_name_project/
    - same_name_project/
    - tests/

# Worked
- different_named_project/
    - a_unique_directory/
    - tests/
Answered By: Henshal B

Better Solution

Try adding a single to your tests directory (a level up from your module) with this contents:

import sys

Your file structure should look like this:


The first append to sys.path will enable you to import from <your-module-name> and the second will enable your packages to import as normal.

In your tests you can import by using from my_module.package import function whereas in your module import using simply from package import function.

Edit: Seems like this solution is not universal (like the others).

I was able to solve this issue using help from this answer.
Add an to your main module directory that contains

import pathlib, sys

I also added another to my tests directory (thanks to this answer) with

import sys
Answered By: Wesley Cheek
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.