How to Install SQLAlchemy in Python

v2.0.49 General Purpose Python >=3.7 MIT

Database Abstraction Library

Install pip install SQLAlchemy

What is SQLAlchemy?

Database Abstraction Library

The Python SQL Toolkit and Object Relational Mapper

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. SQLAlchemy provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

An industrial strength ORM, built from the core on the identity map, unit of work, and data mapper patterns. These patterns allow transparent persistence of objects using a declarative configuration system. Domain models can be constructed and manipulated naturally, and changes are synchronized with the current transaction automatically. A relationally-oriented query system, exposing the full range of SQL's capabilities explicitly, including joins, subqueries, correlation, and most everything else, in terms of the object model. Writing queries with the ORM uses the same techniques of relational composition you use when writing SQL. While you can drop into literal SQL at any time, it's virtually never needed. A comprehensive and flexible system of eager loading for related collections and objects. Collections are cached within a session, and can be loaded on individual access, all at once using joins, or by query per collection across the full result set. A Core SQL construction system and DBAPI interaction layer. The SQLAlchemy Core is separate from the ORM and is a full database abstraction layer in its own right, and includes an extensible Python-based SQL expression language, schema metadata, connection pooling, type coercion, and custom types. All primary and foreign key constraints are assumed to be composite and natural. Surrogate integer primary keys are of course still the norm, but SQLAlchemy never assumes or hardcodes to this model. Database introspection and generation. Database schemas can be "reflected" in one step into Python structures representing database metadata; those same structures can then generate CREATE statements right back out - all within the Core, independent of the ORM.

Quick Start

Minimal example to get started with SQLAlchemy:

from sqlalchemy import create_engine, text

engine = create_engine("sqlite:///example.db")

with engine.connect() as conn:
    conn.execute(text("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)"))
    conn.execute(text("INSERT INTO users (name) VALUES ('Alice')"))
    conn.commit()
    rows = conn.execute(text("SELECT * FROM users")).fetchall()
    print(rows)  # [(1, 'Alice')]

Installation

pip (standard)

pip install SQLAlchemy

Virtual environment (recommended)

python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install SQLAlchemy

pip3

pip3 install SQLAlchemy

conda

conda install -c conda-forge SQLAlchemy

Poetry

poetry add SQLAlchemy

Dependencies

Installing SQLAlchemy will also install these packages:

Verify the Installation

After installing, confirm the package is available:

python -c "import sqlalchemy; print(sqlalchemy.__version__)"

If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.

Installation Errors

Common errors when installing SQLAlchemy with pip.

ModuleNotFoundError: No module named 'sqlalchemy'

Cause: The package is not installed in the current Python environment.

Fix: Run pip install SQLAlchemy. If using a virtual environment, ensure it is activated first.

ModuleNotFoundError: No module named 'sqlalchemy' (installed but still failing)

Cause: pip installed the package into a different Python than the one running your script.

Fix: Use python -m pip install SQLAlchemy to install into the interpreter you are running.

ImportError: cannot import name 'X' from 'sqlalchemy'

Cause: The function or class does not exist in the installed version.

Fix: Check the version with pip show SQLAlchemy and upgrade with pip install --upgrade SQLAlchemy.

pip: command not found

Cause: pip is not in PATH or Python was not added to PATH during installation.

Fix: Try python -m pip install SQLAlchemy. On macOS/Linux try pip3.

PermissionError: [Errno 13] Permission denied

Cause: No write access to the system Python package directory.

Fix: Use a virtual environment, or add --user: pip install --user SQLAlchemy

SSL: CERTIFICATE_VERIFY_FAILED

Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.

Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org SQLAlchemy

Runtime Errors

Common errors when using SQLAlchemy after installation.

sqlalchemy.exc.OperationalError: no such table

Cause: The table has not been created in the database.

Fix: Run Base.metadata.create_all(engine) before using any ORM models.

sqlalchemy.exc.IntegrityError: UNIQUE constraint failed

Cause: Inserting a record violates a unique constraint.

Fix: Check for existing records before insert, or handle the exception and update instead.

sqlalchemy.orm.exc.NoResultFound

Cause: <code>.one()</code> returned zero rows.

Fix: Use .one_or_none() to return None instead of raising, or .first().

Recent Releases

VersionReleased
2.0.49 latest 2026-04-03
2.0.48 2026-03-02
2.0.47 2026-02-24
2.0.46 2026-01-21
2.1.0b1 2026-01-21

Full release history on PyPI →

Manage SQLAlchemy

Upgrade to latest version

pip install --upgrade SQLAlchemy

Install a specific version

pip install SQLAlchemy==2.0.49

Uninstall

pip uninstall SQLAlchemy

Check what is installed

pip show SQLAlchemy

Last updated: 2026-04-11 • Data from PyPI