ModuleNotFoundError in FastAPI Project When Running Pytest

juancos

Juan Jose Pardo

Posted on November 21, 2024

ModuleNotFoundError in FastAPI Project When Running Pytest

Hi everyone,
I'm facing an issue while running Pytest in my FastAPI project, and I would appreciate any guidance. Here's the situation:

thor_software/
├── app/
│ ├── init.py
│ ├── base.py
│ ├── database.py
│ ├── models/
│ │ ├── init.py
│ │ ├── models.py
│ │ ├── schemas.py
├── Tests/
│ ├── init.py
│ ├── test_auth.py
│ ├── test_company_routes.py
├── main.py

When I run pytest in the Test/ directory, I encountered the following error:
ModuleNotFoundError: No module named 'app.models'

What I have done:

  1. Set PYTHONPATH to the project directory.
  2. Confirmed that the app directory contains an init.py file.
  3. Verified the imports in my code. For example:
    from app.models import User, Company

  4. Checked that my file system is APFS (case-insensitive). My project folder name appears as thor_software in the terminal, but in VSCode, it's displayed as THOR_SOFTWARE. Not sure if this is related.

What I Need:
Why might Pytest not recognize app.models, even though the PYTHONPATH is correctly set?
Could the case sensitivity of the file system be affecting the imports?
Any suggestions on how to debug or resolve this issue?

💖 💪 🙅 🚩
juancos
Juan Jose Pardo

Posted on November 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related