I stumbled into a strange error when running pytest in a side-project:

import file mismatch:
imported module 'test_comments' has this __file__ attribute:
  /<root>/django-peterbecom/peterbecom/api/tests/test_comments.py
which is not the same as the test file we want to collect:
  /<root>/django-peterbecom/peterbecom/publicapi/tests/test_comments.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

I desperately made sure I deleted every __pycache__ directory and every *.pyc file. I also ran rm -fr .pytest_cache.

It was strange because I knew I had, in the past, had two files called exactly the same. E.g. test_utils.py.

Turns out, you can have two test_something.py called exactly the same but they must both sit in a directory that has a __init__.py.

So there was nothing wrong with /<root>/django-peterbecom/peterbecom/api/tests/test_comments.py. I just had to do:


touch /<root>/django-peterbecom/peterbecom/api/tests/__init__.py

...and the problem got solved.

Comments

Your email will never ever be published.

Previous:
Copy the current line in VS Code March 12, 2026 macOS
Related by category:
A Python dict that can report which keys you did not use June 12, 2025 Python
logger.error or logger.exception in Python March 6, 2026 Python
Native connection pooling in Django 5 with PostgreSQL June 25, 2025 Python
Using AI to rewrite blog post comments November 12, 2025 Python
Related by keyword:
hashin 0.14.0 with --update-all and a bunch of other features November 13, 2018 Python, Linux