PydanticImportError: BaseSettings has been moved to the pydantic-settings package — how to fix it
PydanticImportError: BaseSettings has been moved to the pydantic-settings package means code written for Pydantic 1 is running on Pydantic 2, which moved BaseSettings into a separate package. Add pydantic-settings to requirements.txt, import it with from pydantic_settings import BaseSettings, and move class Config to model_config = SettingsConfigDict(...). Pinning pydantic<2 also gets the app running, but only as a stopgap.
By Livops team, the people who build and run LivopsUpdated
The app ran on your laptop, and after a fresh deploy it stops on its first import. With Pydantic 2.13 the traceback ends like this:
Traceback (most recent call last):
File ".../config.py", line 1, in <module>
from pydantic import BaseSettings
File ".../site-packages/pydantic/__init__.py", line 437, in __getattr__
return _getattr_migration(attr_name)
File ".../site-packages/pydantic/_migration.py", line 304, in wrapper
raise PydanticImportError(
pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.13/migration/#basesettings-has-moved-to-pydantic-settings for more details.
For further information visit https://errors.pydantic.dev/2.13/u/import-error
The link names your Pydantic version, so it differs from one install to the next. Up to 2.13 it points at docs.pydantic.dev/<version>/migration/[2]; since 2.14 the same message points at pydantic.dev/docs/validation/<version>/migration/[1]. The last line is added to every PydanticImportError[3]. This page explains why the import fails, why it fails only on the server, the fix (pydantic-settings and the new import), the Pydantic 2 settings changes that usually fail next, and when pinning Pydantic 1 is acceptable.
What does the error mean?
BaseSettings, the class that reads settings from environment variables, was part of Pydantic 1: from pydantic import BaseSettings[6]. Pydantic 2 moved it to a separate package, pydantic-settings[4]. Pydantic 2.0 and pydantic-settings 2.0.0 were both released on 30 June 2023[7][8].
Pydantic 2 did not just drop the name. When code asks the pydantic module for an attribute it does not have, Pydantic’s migration hook looks the name up; for BaseSettings it raises PydanticImportError with the message above[1]. PydanticImportError is a subclass of Python’s ImportError[3], so the import line fails before any of your code runs.
Why it breaks on deploy and not locally
The usual requirements.txt that causes this names pydantic with no version, or only a minimum. Two installs of it then end differently:
- Your laptop installed Pydantic 1 when the project started.
pip installprefers to leave the installed version as it is unless--upgradeis given[13], so installing the same file again keeps Pydantic 1. - A fresh environment has nothing installed. pip starts from the most recent version of a package[14], so it takes the newest Pydantic, which is the 2.x line; the current release is 2.14.4[7].
A newer library can also pull Pydantic 2 in for you: the current FastAPI requires pydantic>=2.9.0, and aiogram 3.34.0 requires pydantic>=2.8.0,<2.15[11][12]. Upgrading one of them upgrades Pydantic under your settings class.
Fix 1: install pydantic-settings and change the import
This is the real fix. Add the package to requirements.txt (the name has a dash):
pydantic>=2
pydantic-settings
and import it from its new home (the module has an underscore)[5]:
# before (Pydantic 1)
from pydantic import BaseSettings
# after (Pydantic 2)
from pydantic_settings import BaseSettings, SettingsConfigDict
pydantic-settings depends on pydantic>=2.7.0 and on python-dotenv[8], so reading a .env file needs nothing else installed.
Fix 2: the Pydantic 2 settings changes that fail next
With the import fixed, a settings class written for Pydantic 1 often fails on its next line. These are the changes that usually show up.
class Config becomes model_config
In Pydantic 2, configuration is a model_config class attribute; the Pydantic 1 inner class Config is deprecated[4]. For settings, the dictionary is SettingsConfigDict, and env_file moves into it[5][6]:
# Pydantic 1
from pydantic import BaseSettings
class Settings(BaseSettings):
bot_token: str
database_url: str
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
# Pydantic 2
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")
bot_token: str
database_url: str
Environment variable names are case-insensitive by default, so bot_token reads BOT_TOKEN; env_prefix and case_sensitive also go into SettingsConfigDict[5].
Extra values in .env are now an error
pydantic-settings applies extra='forbid' by default, and to a dotenv file too: an entry in .env with no matching field raises a ValidationError when the settings are built[5]. Each such entry is reported as Extra inputs are not permitted[10]. The pydantic-settings docs give extra='ignore' as the setting compatible with Pydantic 1[5]:
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
Field(env=…) becomes validation_alias
Pydantic 1 set a field’s variable name with Field(..., env=...)[6]. Pydantic 2’s Field no longer accepts arbitrary keyword arguments[4]; the variable name is set with validation_alias[5]:
from pydantic import Field
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
token: str = Field(validation_alias="BOT_TOKEN")
A custom parse_env_var
Pydantic 2 removed the parse_env_var classmethod; to parse values your own way, customise the settings sources[4].
For the rest of a Pydantic 1 codebase (.dict(), validators and so on), the migration guide lists every change and points to bump-pydantic, a tool in beta that rewrites code for Pydantic 2[4].
Stopgap: stay on Pydantic 1 for now
If the app has to be up today and the migration has to wait, there are two short-term options. Both are stopgaps: they buy time, they do not fix the code.
Pin Pydantic below 2. The migration guide gives pydantic==1.* as the way to install the latest Pydantic 1[4]. In requirements.txt:
pydantic<2
Better still, pin the exact version that runs on your laptop (pip show pydantic). This keeps working only while nothing else in your requirements needs Pydantic 2: the current FastAPI and aiogram 3.34 both do[11][12], and pip cannot install two Pydantic versions at once. You are also pinned to the old line while the libraries around you move on.
Import the Pydantic 1 class from Pydantic 2. Pydantic 2 still ships the Pydantic 1 API under pydantic.v1[4], and BaseSettings is part of it[9]:
from pydantic.v1 import BaseSettings
That keeps your class Config and Field(env=...) working unchanged, on Pydantic 2. Treat it as a bridge to Fix 1, not a place to stay.
How to reproduce it locally
The error hides on your laptop because Pydantic 1 is still installed. Do what a fresh server does:
python3 -m venv /tmp/fresh-venv
/tmp/fresh-venv/bin/python -m pip install -r requirements.txt
/tmp/fresh-venv/bin/python -m pip show pydantic
/tmp/fresh-venv/bin/python -c "import config"
Replace config with the module that defines your settings. If pip show prints a 2.x version and the import fails with the same PydanticImportError, you are seeing what the server sees. After the fix, delete /tmp/fresh-venv, run the same lines again, and the import should finish without an error.
On Livops
- Dependencies are installed on the server. Livops installs your
requirements.txtitself on every build, into a new.venv, so an unpinnedpydanticcan resolve to a newer version on any deploy. Addpydantic-settings(or the pin) torequirements.txtand the next deploy installs it. - Do not upload your
.venv. When you upload a folder,venvand.venvare dropped automatically; we install dependencies ourselves. - A failed start. This error stops the app as it starts, so the deploy fails as
start_failed, with the traceback on the Logs page. A web app’s previous version keeps running; a bot’s old version is stopped before the new one starts, so a bot is down until a deploy succeeds. See The app did not start. - Settings come from Variables, not .env. An
.envfile does not reach the repository: put the values under Settings → Variables, and the service gets them as environment variables when it starts. pydantic-settings reads environment variables, and they take priority over a dotenv file[5]. A required field with no value fails with pydantic’sField required[10]: add the variable and deploy again. See Variables. - Two Pydantic pins that contradict each other. If you pin
pydantic<2next to a library that needs Pydantic 2, the install fails withResolutionImpossible; see Install failed.
A Python web app costs 39 credits a month, and a bot 19 credits a month; see Pricing. When the error is No module named 'pydantic_settings', the package is not installed at all: see ModuleNotFoundError: No module named.
Questions
Why did this start after a deploy when nothing changed in my code?
Your requirements.txt names pydantic without a version, or with only a minimum. Your laptop still has the Pydantic 1 it installed long ago, and pip leaves an installed version alone unless you pass --upgrade. A fresh install takes the newest version, which is Pydantic 2, and Pydantic 2 has no BaseSettings.
Which package do I install, pydantic-settings or pydantic_settings?
Install pydantic-settings, with a dash: that is its name on PyPI and the name for requirements.txt. Import it with an underscore: from pydantic_settings import BaseSettings.
Is pinning pydantic below 2 a real fix?
No, it is a stopgap. It gets the app running today, but Pydantic 1 is the old line, new releases of other libraries require Pydantic 2 (current FastAPI asks for pydantic 2.9 or newer, aiogram 3.34 for 2.8 or newer), and pip cannot install both. Plan the move to pydantic-settings.
I fixed the import and now get Extra inputs are not permitted. Why?
pydantic-settings forbids extra values by default, and that includes entries in your .env file that have no matching field. Add the fields, or set extra='ignore' in model_config, which the pydantic-settings docs give as the setting compatible with Pydantic 1.
My Field(..., env='BOT_TOKEN') stopped working. What replaces env=?
validation_alias. In Pydantic 2, Field(validation_alias='BOT_TOKEN') sets the environment variable a field reads. Pydantic 2 no longer accepts arbitrary keyword arguments on Field, so env= no longer names the variable.
Can I keep the Pydantic 1 BaseSettings on Pydantic 2?
For a while: Pydantic 2 still ships the old API under pydantic.v1, so from pydantic.v1 import BaseSettings works. It keeps the old behaviour, but it is a bridge, not a destination.
Sources
- Pydantic source — pydantic/_migration.py (2.14.4) (raw.githubusercontent.com). Checked .
- Pydantic source — pydantic/_migration.py (2.13.0) (raw.githubusercontent.com). Checked .
- Pydantic source — pydantic/errors.py (2.14.4) (raw.githubusercontent.com). Checked .
- Pydantic docs — Migration Guide (pydantic.dev). Checked .
- Pydantic docs — Settings Management (pydantic-settings) (pydantic.dev). Checked .
- Pydantic V1.10 docs — Settings management (pydantic.dev). Checked .
- PyPI — pydantic, project metadata and release history (JSON API) (pypi.org). Checked .
- PyPI — pydantic-settings, project metadata and release history (JSON API) (pypi.org). Checked .
- Pydantic source — pydantic/v1/__init__.py (2.14.4) (raw.githubusercontent.com). Checked .
- Pydantic source — pydantic-core/src/errors/types.rs (2.14.4) (raw.githubusercontent.com). Checked .
- PyPI — fastapi, project metadata (JSON API) (pypi.org). Checked .
- PyPI — aiogram, project metadata (JSON API) (pypi.org). Checked .
- pip docs — pip install (pip.pypa.io). Checked .
- pip docs — Dependency Resolution (pip.pypa.io). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your Python appRelated
- FastAPI hosting in 2026: how to deploy a FastAPI app, and whereA FastAPI app in production runs on Uvicorn without --reload, listens on 0.0.0.0 and the platform's port, trusts the proxy's headers and runs its migrations before it starts. On Livops a repository with main.py and requirements.txt starts on Uvicorn from a push, for 39 credits a month (39,000 so'm). Here is how Render, Railway, Fly.io and a VPS compare.
- ModuleNotFoundError: No module named — after a deployPython searched sys.path and could not find the module you import. After a deploy that almost always means the package is not in requirements.txt, is listed under a different name than the one you import, or the start command runs a Python that the packages were not installed into.
- ImportError: cannot import name 'url_quote' from 'werkzeug.urls': how to fix itImportError: cannot import name 'url_quote' from 'werkzeug.urls' means a Flask from 2.0 to 2.2.3 was installed next to Werkzeug 3, which removed url_quote and almost all of werkzeug.urls. Flask 2.x asks only for a minimum Werkzeug, so a fresh install on the server picks the newest one while your laptop keeps the old one. Pin the Werkzeug that works (Werkzeug<3), or upgrade to Flask 3 together with your extensions.
- ImportError: cannot import name 'executor' from 'aiogram': how to fix itImportError: cannot import name 'executor' from 'aiogram' means code written for aiogram 2 is running on aiogram 3, which removed executor entirely. Rewrite the start for aiogram 3: Dispatcher() without the bot, handlers registered with @dp.message, and await dp.start_polling(bot) inside asyncio.run(main()). Pinning aiogram below 3 only postpones that rewrite.
- pip: Could not find a version that satisfies the requirement / No matching distribution found — how to fix itERROR: Could not find a version that satisfies the requirement X (from versions: none), followed by No matching distribution found for X, means pip searched the package index and found no release of X it can install here. The name is wrong (often the import name instead of the package name), the pinned version does not exist, every release needs a different Python, no file fits your platform, or pip is looking at the wrong index or cannot reach it. The versions list in the message tells you which.
- Error loading ASGI app. Could not import module "main": how to fix UvicornUvicorn prints Error loading ASGI app. Could not import module "main" when Python cannot find the module named before the colon in uvicorn main:app, searching from the directory Uvicorn started in. Make the import string match your files (app/main.py is app.main:app), start from the folder above the package or pass --app-dir, and put the app object's real name after the colon.
- aiogram bot hosting: how to deploy an aiogram 3 botAn aiogram 3 bot needs one process that never stops, the token in an environment variable, state kept outside memory and a clean exit on SIGTERM. On Livops you push a repository with bot.py and requirements.txt, and it runs for 19 credits a month (19,000 so'm), polling or webhook.