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

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

我在 FastAPI 中使用环境变量,但在 运行 服务器 uvicorn main:app --reload:

时出现“ModuleNotFoundError”错误
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'

这是我的项目树:


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

这些是文件:

# 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()

我想知道问题是什么以及如何解决?

假设您 运行 uvicorn main:app --reload 来自 fastapi-alembic/app 目录,您的 main.py 需要将您的设置导入为 from core.config import settings

即将您的 main.py 更新为:

# 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",
)

由于您似乎是来自 app 目录的 运行,因此您不需要在导入语句中包含 app