ImportError: cannot import name 'BaseSettings' from 'pydantic'
Fixes ImportError: cannot import name 'BaseSettings' from 'pydantic' Use when pydantic v2 installed and code does `from pydantic import BaseSettings` Not for: pydantic v1 (BaseSettings is right there; do not add pydantic-settings)
How to fix ImportError: cannot import name 'BaseSettings' from 'pydantic'
Pydantic v2 moved settings out of the main package. Install pydantic-settings and change the import to from pydantic_settings import BaseSettings. Keep from pydantic import Field as-is. That is the whole migration for this error.
The exact error
ImportError: cannot import name 'BaseSettings' from 'pydantic'Fix it
python -m pip install pydantic-settings
Expected: installed
- Change
from pydantic import BaseSettingstofrom pydantic_settings import BaseSettings.
Expected: import works
- Add
pydantic-settingsto requirements.txt / pyproject.
Expected: deploys stop breaking
When this applies
pydantic v2 installed and code does from pydantic import BaseSettings
When this does NOT apply
pydantic v1 (BaseSettings is right there; do not add pydantic-settings)
Versions
pydantic v2
Why it happens
v2 split settings into its own distribution to slim the core.
Edge cases
pydantic-settings v1 is the v1-compat line; v2 line up with pydantic v2.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.