Python

Summary

There are several ways to use Dolt with Python. All require installing the dolt command line tool. Dolt's MySQL client/server support has the most development attention, but Dolt's CLI commands are a simpler way to get started with Python.
The sections below detail "sql-server" and "filesystem" interfaces for working with Dolt in Python. Refer to the CLI vs. server summary for a side by side comparison of how the data interface affects application flexibility.
More information regarding SQL-server use can be found in the Python SQL Client guide.
More information regarding Dolt CLI use in Python use can be found in the Doltpy guides. If interested in the source code, this file is responsible for most of basic commands. Most read/write source code is here.

SQL-Server

Using a MySQL client driver to interact with a Dolt sql-server is the preferred way of using Dolt with Python. At production scale, Dolt is more practical as a Postgres or RDS substitute with additional versioning features.
Several existing Python client packages communicate with SQL servers and integrate with Dolt:
Practical examples using Dolt sql-server:

Filesystem

Contrary to the SQL-Server setup, Dolt's CLI is an all-in-one package and the easiest way to jump into experimenting. Dolt standalone contains all the tools needed to create and mutate databases.
If so inclined, you could use subprocess.Popen to directly call dolt shell commands from Python:
1
$ python3
2
>>> import subprocess
3
>>> subprocess.run(["dolt", "clone", "max-hoffman/qm9"])
4
cloning https://doltremoteapi.dolthub.com/max-hoffman/qm9
5
50,437 of 50,437 chunks complete. 0 chunks being downloaded currently.
6
CompletedProcess(args=['dolt', 'clone', 'max-hoffman/qm9'], returncode=0)
Copied!
We built wrapper librares (doltpy, doltcli) to simplify this experience in Python:
1
$ pip install --user doltcli
2
$ python
3
>>> import doltcli as dolt
4
>>> dolt.Dolt.clone("max-hoffman/qm9")
5
Dolt(repo_dir='/Users/max-hoffman/qm9', print_output=False)
Copied!
Dolt's Python libraries have their own heirarchy and usage patterns. doltcli is dependencyless and minimizes package conflicts for those getting started with Dolt locally. doltpy has more features to integrate with tools in the Python ecosystem. For example, doltpy.cli.write_pandas helps users import Pandas.DataFrame tables into Dolt:
1
$ pip install --user doltpy
2
$ python
3
>>> import pandas as pd
4
>>> import doltpy.cli as dolt
5
>>> from doltpy.cli.write import write_pandas
6
>>>
7
>>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
8
>>> db = dolt.Dolt.init()
9
>>> write_pandas(dolt=db, table="users", df=df, import_mode="create", primary_key=["name"])
10
>>> db.sql("select * from users where name = 'User 1'", result_format="json")
11
{'rows': [{'name': 'User 1'}]}
Copied!
Last modified 5mo ago
Export as PDF
Copy link