'How to fix "ModuleNotFoundError: No module named 'app' " while running a FastAPI server?

I'm using environment variables with FastAPI, but I'm getting a "ModuleNotFoundError" error while running the server with uvicorn main:app --reload:

Traceback (most recent call last):
  File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/home/user/.pyenv/versions/3.9.2/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/home/user/.pyenv/versions/3.9.2/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 67, in serve
    config.load()
  File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/config.py", line 458, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 24, in import_from_string
    raise exc from None
  File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/home/user/.pyenv/versions/3.9.2/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "./main.py", line 3, in <module>
    from app.core.config import settings
ModuleNotFoundError: No module named 'app'

This is my project tree:


  |__fastapi-alembic 
     |__alembic 
     |__app
        ├── core
        │   └── config.py
        └── __init__.py
        └── main.py
     |__alembic.ini
     |__poetry.lock
     |__pyproject.toml

And these are the files:

# main.py
from fastapi import FastAPI
from app.core.config import settings


app = FastAPI(
    title=settings.PROJECT_NAME,
    openapi_url=f"{settings.API_V1_STR}/openapi.json",
)
# config.py
import os
from pydantic import BaseSettings

class Settings(BaseSettings):
    API_V1_STR: str = "/api/v1"
    PROJECT_NAME: str = os.environ["PROJECT_NAME"]


settings = Settings()

I would like to know what the problem is & how could I solve it?



Solution 1:[1]

Assuming you're running uvicorn main:app --reload from the fastapi-alembic/app directory, your main.py needs to import your settings as from core.config import settings.

i.e. update your main.py to:

# main.py
from fastapi import FastAPI
from core.config import settings  # This import here is what you want to update


app = FastAPI(
    title=settings.PROJECT_NAME,
    openapi_url=f"{settings.API_V1_STR}/openapi.json",
)

As you appear to be running from within the app directory, you don't need to include app in your import statements.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Josh