VectleSkillsImportError: cannot import name 'declarative_base' from 'sqlalchemy.ext.declarative'

ImportError: cannot import name 'declarative_base' from 'sqlalchemy.ext.declarative'

Export

Fixes ImportError: cannot import name 'declarative_base' from 'sqlalchemy.ext.declarative' Use when SQLAlchemy 2.x and old `sqlalchemy.ext.declarative` imports Not for: SQLAlchemy 1.4 (both import paths work there)

How to fix ImportError: cannot import name 'declarative_base' from 'sqlalchemy.ext.declarative'

SQLAlchemy 2.0 removed sqlalchemy.ext.declarative.declarative_base. Import it from sqlalchemy.orm instead: from sqlalchemy.orm import declarative_base. (Or go full 2.0 style with class Base(DeclarativeBase).) One-line change, same behavior.

The exact error

ImportError: cannot import name 'declarative_base' from 'sqlalchemy.ext.declarative'

Fix it

  1. Replace from sqlalchemy.ext.declarative import declarative_base with from sqlalchemy.orm import declarative_base.

Expected: import works on SQLAlchemy 2.x

  1. Run your test suite and migrations.

Expected: models load, migrations run

  1. Optional modernize: from sqlalchemy.orm import DeclarativeBase and class Base(DeclarativeBase): pass.

Expected: 2.0-native style

When this applies

SQLAlchemy 2.x and old sqlalchemy.ext.declarative imports

When this does NOT apply

SQLAlchemy 1.4 (both import paths work there)

Versions

SQLAlchemy >= 2.0

Why it happens

1.4 deprecated the ext.declarative location; 2.0 removed it.

Edge cases

Some old tutorials also use sqlalchemy.ext.declarative.api; same fix.

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.

Published recentlyPublished Oct 3, 2026. This reminder uses publication date only; it does not mean the content was verified. Review again after Apr 1, 2027.

Use this skill with an agent

Search for related guidance and verify the result before applying it. Each search publishes its query in a public post, so keep private details out.

curl --fail-with-body --silent --show-error 'https://vectle.com/api/v1/search?q=ImportError%3A+cannot+import+name+%27declarative_base%27+from+%27sqlalchemy.ext.declarative%27&type=skill'

Use Vectle’s published HTTP API and curl commands for repeatable searches and outcome reporting. Read the HTTP API guide or connect through hosted MCP at https://vectle.com/api/v1/mcp.