question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportError ModuleNotFoundError in pytest

See original GitHub issue

Running python 3.7.3 on mac using pytest

My folder structure/files is src/tests/pn/tests/functional/test_something.py src/tests/pn/tests/functional/config.py src/tests/pn/lib/util.py

PYTHONPATH is set to …src/tests/pn which means ‘pn’ folder is in relative path

In test_something.py I have two import statements

from lib.util import *
from tests/functional/config import *

I run pytest -s tests/functional/test_something.py

Both paths are relative to ‘pn’ folder, import of ‘lib.util’ seems successful…however it fails to import ‘tests/functional/config’ and gives the error

ImportError while importing test module ‘… src/tests/pn/tests/functional/test_something.py’. Hint: make sure your test modules/packages have valid Python names. Traceback: tests/functional/reportgen/test_something.py:23: in <module> from tests.functional.config import * E ModuleNotFoundError: No module named ‘tests.functional’

I am confused how the relative import works for one but fails for the other… Is it because the source file and import file share the same folder hierarchy ‘tests/functional’?

It works when I import with full path like 'from src.tests.pn.tests.functional.config import *

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

15reactions
n3011commented, May 30, 2021

Sometimes when pytest is installed outside your development environment, it can cause this issue. So if you are using virtual/conda environment, then deactivate the environment - then uninstall pytest from the system; then again activate your environment and run the tests.

1reaction
michelgokancommented, Feb 12, 2022

In my case, I deleted __init__.py in the root folder and it resolved an exact same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ModuleNotFoundError with pytest - python - Stack Overflow
You can test this by python -m pytest . If you see ModuleNotFoundError: No module named 'pytest' then your pytest is at the...
Read more >
pytest: ModuleNotFoundError: No module named 'requests'
Fixing pytest and the ModuleNotFoundError · Exit any virtual environment · Use Pip to uninstall pytest · Activate your project's virtual ...
Read more >
[Fixed] No module named 'pytest' - Finxter
Specifically, Python raises the ModuleNotFoundError if the module (e.g., pytest ) cannot be found. If it can be found, there may be a...
Read more >
No module named 'pytest' after Upgrade to PyCharm 2018.3
ModuleNotFoundError: No module named 'pytest' after Upgrade to PyCharm 2018.3 ... What steps will reproduce the problem? ... This seems to happen with...
Read more >
ModuleNotFoundError When Trying to use PyTest - Reddit
Removing conftest.py. Emptying __init__.py. Removing the . from from .my_app import * (causes a circular import error).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found