Py.test No module named *


I have a folder structure like this


In my file, I have a line to import my app module. When I run py.test on the root folder, I get this error about no module named app. How should I configure this?

Asked By: zsljulius



So you are running py.test from /App. Are you sure /App/App is in your $PYTHONPATH?

If it’s not, code that tries to import app will fail with such a message.

EDIT0: including the info from my comment below, for completeness.

An attempt to import app will only succeed if it was executed inside /App/App, which is not the case here. You probably want to make /App/App a package by putting inside it, and change your import to qualify app as from App import app.

EDIT1: by request, adding further explanation from my second comment below.

By putting inside /App/App, that directory becomes a package. Which means you can import from it, as long as it – the directory – is visible in the $PYTHONPATH. I.e. you can do from App import app if /App is in the $PYTHONPATH. Your current working directory gets automatically added to $PYTHONPATH, so when you run a script from /App, the import will work.

Answered By: Dun Peal

I already had an file in the /App/App directory and wanted to run tests from the project root without any path-mangling magic:

 python -m pytest tests

The output immediately looks like this:

➟  python -m pytest tests
====================================== test session starts ======================================
platform linux -- Python 3.5.1, pytest-2.9.0, py-1.4.31, pluggy-0.3.1
rootdir: /home/andrew/code/app, inifile: 
plugins: teamcity-messages-1.17
collected 46 items 

... lines omitted ...

============================= 44 passed, 2 skipped in 1.61 seconds ==============================
Answered By: berezovskyi

Working with Python 3 and getting the same error on a similar project layout, I solved it by adding an __init__ file to my tests module.

$ touch tests/

I’m not great at packaging and importing, but I think that this helps pytest work out where the target App module is located.

Answered By: jamesc

I also got same error while running test cases for my app located as below


I deleted my myproject’s file to run my test cases.

Answered By: Jay

I got the similar issue. And after trying multiple things including installing pytest on virtual environment and adding/removing file from the package, none worked for me.

Solution that worked for me is(windows solution):
Added Project folder(not package folder) to python path(set PYTHONPATH=%PYTHONPATH%;%CD%)

Ran my script from Project Folder and boom, it worked.

Answered By: user1734234

I had a similar problem and had to delete from the root and add an to the tests folder.

Answered By: Tiago De Souza

Running pytest with the python -m pytest command helps with this exact thing.

Since your current package is not yet in your $PYTHONPATH or sys.path – pytest gets this error.

By using python -m pytest you automatically add the working directory into sys.path for running pytest. Their documentation also mentions:

This is almost equivalent to invoking the command line script pytest

Answered By: nchopra

This worked for me:
Went to parent app, and pip install -e . (install a local and editable app).

Answered By: Yishai Landau


You might as well add an empty file to your root app folder.

(if you take a look at the question folder structure, that would be the same level as the "Tests" folder, not inside of it).

More info:

Pytest looks for files inside all your project folders. provides configuration for the file tree pytest finds it in. Because pytest somehow scans all subdirectories starting from folder, it should find packages/modules outside the tests folder (as long as a file is in your app root folder).

Eventually, you might want to write some code in your empty, specially to share fixtures among different tests files, in order to avoid duplicate code. Afterall, the DRY principle (Don’t Repeat Yourself) should also be follwed when writing tests.

Adding __init.py__ to the tests folder also should help pytest to find modules throughout your application. However, note that Python 3.3+ has implicit namespace packages that allow it to create a packages without an file. That been said, creating files for this specific purpose seems more like a a workaround for pytest than a python requirement. More about that in: Is not required for packages in Python 3.3+

Answered By: Sverissimo

What worked for me: I had to make absolute imports in my test file and call python -m test in the root folder.

Answered By: Luis Felipe

I hit the same issue.


I set the files. I added a ( for sharing pytest.fixtures ). I added this to my Poetry file ( pyproject.toml:

pythonpath = [

Turned out it was my use of hyphens and not underscores ! Noo…

# pytest can't find Module

# works
Answered By: rustyMagnet
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.