|
1 | 1 | --- |
2 | 2 | title: Turso Quickstart (Python) |
3 | 3 | sidebarTitle: Quickstart |
4 | | -description: Get started with Turso and Python using the libSQL client in a few simple steps. |
| 4 | +description: Get started with Turso and Python in a few simple steps. |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | In this Python quickstart we will learn how to: |
8 | 8 |
|
9 | | -- Retrieve database credentials |
10 | | -- Install the libSQL package |
11 | | -- Connect to a Turso database |
| 9 | +- Install the Turso package |
| 10 | +- Connect to a local or remote database |
12 | 11 | - Execute a query using SQL |
13 | 12 | - Sync changes to local database |
14 | 13 |
|
| 14 | +## Recommended: pyturso (Local / Embedded) |
| 15 | + |
| 16 | +`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. |
| 17 | + |
15 | 18 | <Steps> |
16 | | - <Step title="Retrieve database credentials"> |
| 19 | + <Step title="Install"> |
17 | 20 |
|
18 | | -You will need an existing database to continue. If you don't have one, [create one](/quickstart). |
| 21 | +```bash |
| 22 | +uv add pyturso |
| 23 | +``` |
19 | 24 |
|
20 | | -<Snippet file="retrieve-database-credentials.mdx" /> |
| 25 | +Or with pip: |
21 | 26 |
|
22 | | -<Info>You will want to store these as environment variables.</Info> |
| 27 | +```bash |
| 28 | +pip install pyturso |
| 29 | +``` |
23 | 30 |
|
24 | 31 | </Step> |
| 32 | + <Step title="Connect"> |
25 | 33 |
|
26 | | - <Step title="Install"> |
| 34 | +```py |
| 35 | +import turso |
27 | 36 |
|
28 | | -First begin by adding libSQL to your project: |
| 37 | +db = turso.connect("app.db") |
| 38 | +``` |
29 | 39 |
|
30 | | - ```bash |
31 | | - pip install libsql |
32 | | - ``` |
| 40 | +In-memory databases are also supported: |
| 41 | + |
| 42 | +```py |
| 43 | +db = turso.connect(":memory:") |
| 44 | +``` |
33 | 45 |
|
34 | 46 | </Step> |
| 47 | + <Step title="Execute"> |
35 | 48 |
|
36 | | - <Step title="Connect"> |
| 49 | +```py |
| 50 | +db.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)") |
| 51 | +db.execute("INSERT INTO users (name) VALUES (?)", ("Alice",)) |
| 52 | +db.commit() |
| 53 | + |
| 54 | +for row in db.execute("SELECT * FROM users"): |
| 55 | + print(row) |
| 56 | +``` |
| 57 | + |
| 58 | + </Step> |
| 59 | + <Step title="Sync (push and pull)"> |
| 60 | + |
| 61 | +If you need to sync your local database with Turso Cloud, use `turso.sync`: |
| 62 | + |
| 63 | +```py |
| 64 | +import os |
| 65 | +import turso.sync |
37 | 66 |
|
38 | | -Then import the package: |
| 67 | +db = turso.sync.connect( |
| 68 | + "app.db", |
| 69 | + remote_url=os.environ["TURSO_DATABASE_URL"], |
| 70 | + auth_token=os.environ["TURSO_AUTH_TOKEN"], |
| 71 | +) |
39 | 72 |
|
40 | | - ```py |
41 | | - import libsql |
42 | | - ``` |
| 73 | +db.execute("INSERT INTO users (name) VALUES (?)", ("Bob",)) |
| 74 | +db.commit() |
43 | 75 |
|
44 | | -Now connect to your local or remote database using the libSQL connector: |
| 76 | +# Push local writes to Turso Cloud |
| 77 | +db.push() |
45 | 78 |
|
46 | | -<AccordionGroup> |
47 | | - <Accordion title="Embedded Replicas"> |
48 | | - ```py |
49 | | - url = os.getenv("TURSO_DATABASE_URL") |
50 | | - auth_token = os.getenv("TURSO_AUTH_TOKEN") |
| 79 | +# Pull remote changes to local database |
| 80 | +db.pull() |
| 81 | +``` |
51 | 82 |
|
52 | | - conn = libsql.connect("hello.db", sync_url=url, auth_token=auth_token) |
53 | | - conn.sync() |
54 | | - ``` |
| 83 | +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](/sync/usage) for details on conflict resolution, checkpointing, and more. |
55 | 84 |
|
56 | | - </Accordion> |
| 85 | + </Step> |
| 86 | +</Steps> |
57 | 87 |
|
58 | | - <Accordion title="Local only"> |
59 | | - ```py |
60 | | - conn = libsql.connect("hello.db") |
61 | | - cur = conn.cursor() |
62 | | - ``` |
| 88 | +## Remote Access (Over-the-Wire) |
63 | 89 |
|
64 | | - </Accordion> |
| 90 | +If your application needs to query a Turso Cloud database directly over the network (e.g., from a web server or serverless function), you can use the `libsql` package. It connects to your database via HTTP — no local file needed. |
65 | 91 |
|
66 | | - {/* <Accordion title="Remote only"> |
67 | | - ```py |
68 | | - url = os.getenv("TURSO_DATABASE_URL") |
69 | | - auth_token = os.getenv("TURSO_AUTH_TOKEN") |
| 92 | +<Info> |
70 | 93 |
|
71 | | - conn = libsql.connect(database=url, auth_token=auth_token) |
72 | | - ``` |
| 94 | +For most applications, we recommend running a local database with [Turso Sync](/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). |
73 | 95 |
|
74 | | - </Accordion> */} |
75 | | -</AccordionGroup> |
| 96 | +</Info> |
76 | 97 |
|
77 | | - </Step> |
| 98 | +<Steps> |
| 99 | + <Step title="Retrieve database credentials"> |
78 | 100 |
|
79 | | - <Step title="Execute"> |
| 101 | +You will need an existing database to continue. If you don't have one, [create one](/quickstart). |
80 | 102 |
|
81 | | -You can execute SQL queries against your existing database as follows: |
| 103 | +<Snippet file="retrieve-database-credentials.mdx" /> |
82 | 104 |
|
83 | | -```py |
84 | | -conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER);") |
85 | | -conn.execute("INSERT INTO users(id) VALUES (10);") |
| 105 | +<Info>You will want to store these as environment variables.</Info> |
86 | 106 |
|
87 | | -print(conn.execute("select * from users").fetchall()) |
| 107 | + </Step> |
| 108 | + <Step title="Install"> |
| 109 | + |
| 110 | +```bash |
| 111 | +uv add libsql |
| 112 | +``` |
| 113 | + |
| 114 | +Or with pip: |
| 115 | + |
| 116 | +```bash |
| 117 | +pip install libsql |
88 | 118 | ``` |
89 | 119 |
|
90 | 120 | </Step> |
| 121 | + <Step title="Connect and query"> |
| 122 | + |
| 123 | +```py |
| 124 | +import os |
| 125 | +import libsql |
| 126 | + |
| 127 | +conn = libsql.connect( |
| 128 | + database=os.environ["TURSO_DATABASE_URL"], |
| 129 | + auth_token=os.environ["TURSO_AUTH_TOKEN"], |
| 130 | +) |
91 | 131 |
|
92 | | - <Step title="Sync"> |
| 132 | +conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)") |
| 133 | +conn.execute("INSERT INTO users (name) VALUES (?)", ("Alice",)) |
| 134 | +conn.commit() |
93 | 135 |
|
94 | | -If you need to sync your local database with a remote Turso Cloud database (local reads and writes with push/pull to the cloud), use [Turso Sync](/sync/usage). Turso Sync is built on the Turso Database engine and provides true local-first sync with explicit `push()` and `pull()` operations. |
| 136 | +rows = conn.execute("SELECT * FROM users").fetchall() |
| 137 | +print(rows) |
| 138 | +``` |
95 | 139 |
|
96 | 140 | </Step> |
97 | 141 | </Steps> |
| 142 | + |
| 143 | +## Legacy: libsql (Embedded Replicas) |
| 144 | + |
| 145 | +The `libsql` package also supports embedded replicas, where reads are local but **writes go to the remote database**. For true local-first writes with push/pull sync, use `turso.sync` instead. |
| 146 | + |
| 147 | +See the [reference](/sdk/python/reference) for full documentation on embedded replicas, encryption, and periodic sync. |
0 commit comments