liveBook · Manning

DuckDB Python package

As explained in DuckDB in Action

The DuckDB Python API is the programming interface for connecting to DuckDB and executing database operations from Python.

The DuckDB Python package connects Python programs to DuckDB, supports in-memory or file-backed databases, and provides a direct path from Python workflows to SQL operations. section 6.1

A practical setup usually includes:

  • Install the package with pip, import duckdb, and inspect duckdb.__version__. section 6.1.1
  • Open a database file, including read-only mode when an application only queries existing data. section 6.1 section 9.1.2
  • Use SQL from Python to load CSV files, convert column types, and populate tables. section 8.4.1

Querying a file-backed database

import duckdb atp_duck = duckdb.connect('atp.duckdb', read_only=True) query = ''' SELECT DISTINCT winner_name AS player FROM matches WHERE player ilike '%' || $search_term || '%' ''' values = atp_duck.execute(query, {"search_term": "Novak"}).fetchall() section 9.1.2

pip install duckdb

Use the installation command as the quickest entry point before importing and checking the package version. section 6.1.1

How DuckDB in Action covers it

How deep it goes
Covers installation, imports, version checks, connections, file loading, type conversion, and prepared queries without cataloging the full API.
Examples
Demonstrates package installation, ATP match loading, read-only database access, and parameterized player search.
What it assumes
Assumes basic Python and SQL familiarity while teaching package installation, connection choices, and initial database interaction.
What you can do after
Operate/configure DuckDB from Python by installing the package, opening databases, loading CSV data, and running parameterized queries.
How it connects
Ties Python to SQL, CSV files, Streamlit applications, and Dagster-managed data pipelines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.