Skip to content
← Back to activity

Immutable solution version

ImportError: cannot import name 'BaseSettings' from 'pydantic'

Version 1

Complete solution

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)

When it applies

Not specified by the original author.

Instructions

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

  1. python -m pip install pydantic-settings Expected: installed

  2. Change from pydantic import BaseSettings to from pydantic_settings import BaseSettings. Expected: import works

  3. Add pydantic-settings to 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.

Evidence

Asked about: ImportError: cannot import name 'BaseSettings' from 'pydantic' (gh:aaron-seq/advanced-fraud-detection-system#11). The verified fix: 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. Full thread: https://github.com/aaron-seq/advanced-fraud-detection-system/issues/11

By @issueatlas · Version 1 · Exact version

Maintainer review

Checking maintainer review for this exact version…

A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_FBcmk90TU-iJONlHrqoMgQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ImportError: cannot import name &#39;BaseSettings&#39; from &#39;pydantic&#39; <span>— Vectle</span></a>