# Reference

> Python Reference for Turso
> Canonical HTML: https://tursodb.cn/docs/sdk/python/reference/
> Upstream Source (MIT): https://docs.turso.tech/sdk/python/reference
> Language: en

---
Turso offers three Python packages:

| | `pyturso` | `turso_serverless` | `libsql` |
| - | - | - | - |
| **Use case** | Local / embedded database, sync | Remote Turso database (over-the-wire) | Remote libSQL database, existing libSQL codebases |
| **Engine** | Turso (rewrite) | Turso (rewrite) | libSQL (SQLite fork) |
| **Concurrent writes** | Yes (MVCC) | Yes (MVCC) | Not supported |
| **Sync** | push/pull (local-first) | — | Embedded Replicas (writes go to cloud primary) |
| **API** | Python `sqlite3`-compatible | Python `sqlite3`-compatible | Python `sqlite3`-compatible |

**Starting a new project?** Use `pyturso` — it is built on the Turso engine with concurrent writes and local-first sync. For remote access over the network, match the driver to the database engine: [`turso_serverless`](/docs/sdk/python/quickstart/) for Turso databases, `libsql` for libSQL databases.

## pyturso

For local and embedded use. Built on the Turso Database engine with concurrent writes (MVCC) and async I/O.

### Installing

```bash
pip install pyturso
```

### Connecting

```py
import turso

conn = turso.connect("app.db")
```

In-memory databases are also supported:

```py
conn = turso.connect(":memory:")
```

### Querying

```py
conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
conn.execute("INSERT INTO users (name) VALUES (?)", ("Alice",))
conn.commit()

for row in conn.execute("SELECT * FROM users"):
    print(row)
```

### Encryption

Encrypt local databases at rest using the `encryption` option:

```py
from turso import connect, EncryptionOpts

conn = connect("encrypted.db",
               experimental_features="encryption",
               encryption=EncryptionOpts(cipher="aegis256",
                                         hexkey="b1bbfda4f589dc9daaf004fe21111e00dc00c98237102f5c7002a5669fc76327"))
```

Supported ciphers: `aegis256`, `aegis256x2`, `aegis128l`, `aegis128x2`, `aegis128x4`, `aes256gcm`, `aes128gcm`.

Encrypted databases cannot be read as standard SQLite databases — you must use the Turso Database engine to open them.

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Turso Cloud databases can also be encrypted with bring-your-own-key — [learn more](/docs/cloud/encryption/).

</div></div>


## libsql (libSQL)

The `libsql` package is built on [libSQL](https://github.com/tursodatabase/libsql), the open-source fork of SQLite that powers Turso Cloud today. It is production-ready and battle-tested, and is the right choice when you are working with an existing `libsql`-based codebase.

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

With `libsql` Embedded Replicas, reads are local and writes are sent to the cloud primary, then reflected back to the replica. Embedded Replicas are fully supported. For new projects that need sync, we recommend `turso.sync` — see the [quickstart](/docs/sdk/python/quickstart/).

</div></div>


## Embedded Replicas

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

For workloads that need **offline writes**, **bidirectional sync**, or **multi-writer convergence**, we recommend `turso.sync` — both reads and writes are local, and you sync explicitly with `push()` / `pull()`. See the [quickstart](/docs/sdk/python/quickstart/).

</div></div>


You can work with [embedded replicas](https://docs.turso.tech/features/embedded-replicas) that can sync from the remote database to a local SQLite file, and delegate writes to the remote primary database:

```py
import os

import libsql

conn = libsql.connect("local.db", sync_url=os.getenv("LIBSQL_URL"),
                      auth_token=os.getenv("LIBSQL_AUTH_TOKEN"))
conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);")
conn.execute("INSERT INTO users(id) VALUES (1);")
conn.commit()

print(conn.execute("select * from users").fetchall())
```

<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

Embedded Replicas only works where you have access to the file system.

</div></div>

### Periodic Sync

You can automatically sync at intervals by passing time in seconds to the `sync_interval` option. For example, to sync every minute, you can use the following code:

```py
conn = libsql.connect("local.db", sync_interval=60, sync_url=os.getenv("LIBSQL_URL"),
                      auth_token=os.getenv("LIBSQL_AUTH_TOKEN"))
```

### Manual Sync

The `Sync` function allows you to sync manually the local database with the remote counterpart:

```py
conn.execute("INSERT INTO users(id) VALUES (2);")
conn.commit()
conn.sync()
```

## Encryption

<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

For new projects, we recommend [`pyturso`](#pyturso) for local encryption — it is built on the Turso Database engine with better performance and concurrent write support.

</div></div>


To enable encryption on a SQLite file, pass the encryption secret to the `encryption_key` option:

```py
conn = libsql.connect("encrypted.db", sync_url=os.getenv("LIBSQL_URL"),
                      auth_token=os.getenv("LIBSQL_AUTH_TOKEN"),
                      encryption_key=os.getenv("ENCRYPTION_KEY"))
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Encrypted databases appear as raw data and cannot be read as standard SQLite databases. You must use the libSQL client for any operations — [learn more](/docs/libsql/#encryption-at-rest).

</div></div>
