# Turso Quickstart (Python)

> Get started with Turso and Python in a few simple steps.
> Canonical HTML: https://tursodb.cn/docs/sdk/python/quickstart/
> Upstream Source (MIT): https://docs.turso.tech/sdk/python/quickstart
> Language: en

---
In this Python quickstart we will learn how to:

* Install the Turso package
* Connect to a local or remote database
* Execute a query using SQL
* Sync changes to local database

## Recommended: pyturso (Local / Embedded)

`pyturso` is the recommended package for local and embedded use cases. It is built on the Turso Database engine — a ground-up rewrite of SQLite with concurrent writes (MVCC) and async I/O. The API follows the standard Python `sqlite3` interface, so it works as a drop-in replacement.

**1. Install**

```bash
uv add pyturso
```

Or with pip:

```bash
pip install pyturso
```

**2. Connect**

```py
import turso

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

In-memory databases are also supported:

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

**3. Execute**

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

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

**4. Sync (push and pull)**

If you need to sync your local database with Turso Cloud, use `turso.sync`:

```py
import os
import turso.sync

db = turso.sync.connect(
    "app.db",
    remote_url=os.environ["TURSO_DATABASE_URL"],
    auth_token=os.environ["TURSO_AUTH_TOKEN"],
)

db.execute("INSERT INTO users (name) VALUES (?)", ("Bob",))
db.commit()

# Push local writes to Turso Cloud
db.push()

# Pull remote changes to local database
db.pull()
```

All reads and writes happen against the local database file — fast, offline-capable. `push()` sends your changes to the cloud. `pull()` brings remote changes down. See [Turso Sync](/docs/sync/usage/) for details on conflict resolution, checkpointing, and more.

## Remote Access (Over-the-Wire)

If your application needs to query a Turso Cloud database directly over the network (e.g., from a web server or serverless function), use the driver that matches your database engine: `turso_serverless` for [Turso databases](/docs/tursodb/quickstart/), or `libsql` for [libSQL](/docs/libsql/) databases.

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

For most applications, we recommend running a local database with [Turso Sync](/docs/sync/usage/) (`turso.sync`) instead — it gives you faster reads, offline support, and lower latency. Remote access is useful when you cannot store a local database file (e.g., stateless serverless environments).

</div></div>


### Remote Turso database: turso\_serverless

`turso_serverless` connects to a remote Turso database over HTTP — no persistent connections, no native extensions, just the standard library. Like `pyturso`, it implements the standard DB-API interface.

**1. Retrieve database credentials**

You will need an existing Turso database to continue. If you don't have one, create one with `turso db create --tursodb` (see the [quickstart](/docs/quickstart/)).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=turso://[databaseName]-[organizationSlug].turso.io
TURSO_AUTH_TOKEN=
```

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

You will want to store these as environment variables.

</div></div>


**2. Install**

```bash
uv add turso_serverless
```

Or with pip:

```bash
pip install turso_serverless
```

**3. Connect and query**

```py
import os
import turso_serverless

conn = turso_serverless.connect(
    os.environ["TURSO_DATABASE_URL"],
    auth_token=os.environ["TURSO_AUTH_TOKEN"],
)

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)

conn.close()
```

### Remote libSQL database: libsql

`libsql` connects to a remote libSQL database via HTTP — no local file needed.

**1. Retrieve database credentials**

You will need an existing libSQL database to continue. If you don't have one, [create one](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

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

You will want to store these as environment variables.

</div></div>


**2. Install**

```bash
uv add libsql
```

Or with pip:

```bash
pip install libsql
```

**3. Connect and query**

```py
import os
import libsql

conn = libsql.connect(
    database=os.environ["TURSO_DATABASE_URL"],
    auth_token=os.environ["TURSO_AUTH_TOKEN"],
)

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

rows = conn.execute("SELECT * FROM users").fetchall()
print(rows)
```

## Embedded Replicas (libsql)

The `libsql` package also supports Embedded Replicas — local reads from a file, with writes sent to the cloud primary and reflected back to the replica. Embedded Replicas are fully supported in production. For new projects that need sync, we recommend `turso.sync` instead: both reads and writes are local, and you sync explicitly with `push()` / `pull()`.

See the [reference](/docs/sdk/python/reference/) for full documentation on Embedded Replicas, encryption, and periodic sync.
