You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/stable/dev/building/python.md
+40-40Lines changed: 40 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,14 +7,14 @@ title: Python
7
7
8
8
The DuckDB Python package lives in the main [DuckDB source on Github](https://github.com/duckdb/duckdb/) under the `/tools/pythonpkg/` folder. It uses [pybind11](https://pybind11.readthedocs.io/en/stable/) to create Python bindings with DuckDB.
9
9
10
-
# Prerequisites
10
+
##Prerequisites
11
11
12
12
For everything described on this page we make the following assumptions:
13
13
14
14
1. You have a working copy of the duckdb source (including the git tags) and you run commands from the root of the source
15
15
2. You have a suitable Python installation available in a dedicated virtual env
16
16
17
-
## 1. DuckDB code
17
+
###1. DuckDB code
18
18
19
19
Make sure you have checked out the [DuckDB source](https://github.com/duckdb/duckdb/) and that you are in its root. E.g.:
20
20
@@ -38,7 +38,7 @@ git fetch --tags upstream
38
38
git push --tags
39
39
```
40
40
41
-
## 2. Python Virtual Env
41
+
###2. Python Virtual Env
42
42
43
43
For everything described here you will need a suitable Python installation. While you technically might be able to use your system Python, we **strongly** recommend you use a Python virtual environment. A virtual environment isolates dependencies and, depending on the tooling you use, gives you control over which Python interpreter you use. This way you don't pollute your system-wide Python with the different packages you need for your projects.
44
44
@@ -68,27 +68,27 @@ If that fails with `No module named pip` and you use `uv`, then run:
68
68
$ uv pip install pip
69
69
```
70
70
71
-
# Building From Source
71
+
##Building From Source
72
72
73
73
Below are a number of options to build the python library from source, with or without debug symbols, and with a default or custom set of [extensions]({% link docs/stable/extensions/overview.md %}). Make sure to check out the [DuckDB build documentation]({% link docs/stable/dev/building/overview.md %}) if you run into trouble building the DuckDB main library.
74
74
75
-
## Default release, debug build or cloud storage
75
+
###Default release, debug build or cloud storage
76
76
77
77
The following will build the package with the default set of extensions (json, parquet, icu and core_function).
78
78
79
-
### Release build
79
+
####Release build
80
80
81
81
```bash
82
82
GEN=ninja BUILD_PYTHON=1 make release
83
83
```
84
84
85
-
### Debug build
85
+
####Debug build
86
86
87
87
```bash
88
88
GEN=ninja BUILD_PYTHON=1 make debug
89
89
```
90
90
91
-
### Cloud Storage
91
+
####Cloud Storage
92
92
93
93
You may need the package files to reside under the same prefix where the library is installed; e.g., when installing to cloud storage from a notebook.
94
94
@@ -110,13 +110,13 @@ tar --directory=$DUCKDB_PREFIX/src/duckdb-pythonpkg -xzpf tools/pythonpkg/dist/d
Before thinking about statically linking extensions you should know that the Python package currently doesn't handle linked in extensions very well. If you don't really need to have an extension baked in than the advice is to just stick to [installing them at runtime]({% link docs/stable/extensions/installing_extensions.md %}). See `tools/pythonpkg/duckdb_extension_config.cmake` for the default list of extensions that are built with the python package. Any other extension should be considered problematic.
122
122
@@ -131,35 +131,35 @@ The DuckDB build process follows the following logic for building extensions:
131
131
132
132
The following mechanisms add to the set of **_included_ extensions**:
133
133
134
-
| Mechanism | Syntax / Example |
135
-
| ---| ---|
136
-
|**Built-in extensions enabled by default**|`extension/extension_config.cmake` (≈30 built-ins) |
137
-
|**Python package extensions enabled by default**|`tools/pythonpkg/duckdb_extension_config.cmake` (`json;parquet;icu`) |
138
-
|**Semicolon-separated include list**|`DUCKDB_EXTENSIONS=fts;tpch;json`|
|**“No built-ins” switch** <br/>_Throws out *every* statically linked extension **except**`core_functions`. Use `CORE_EXTENSIONS=…` to whitelist a subset back in._|`DISABLE_BUILTIN_EXTENSIONS=1`|
|**“No built-ins” switch** <br/>_Throws out *every* statically linked extension **except**`core_functions`. Use `CORE_EXTENSIONS=…` to whitelist a subset back in._|`DISABLE_BUILTIN_EXTENSIONS=1`|
153
153
154
154
---
155
155
156
-
## Show all installed extensions
156
+
###Show all installed extensions
157
157
158
158
```bash
159
159
python3 -c "import duckdb; print(duckdb.sql('SELECT extension_name, installed, description FROM duckdb_extensions();'))"
160
160
```
161
161
162
-
# Development Environment
162
+
##Development Environment
163
163
164
164
To set up the codebase for development you should run build duckdb as follows:
165
165
@@ -178,7 +178,7 @@ Once the build completes, do a sanity check to make sure everything works:
The basic recipe is to start `lldb` with your virtual env's Python interpreter and your script, then set a breakpoint and run your script.
184
184
@@ -214,11 +214,11 @@ WARNING: Unable to resolve breakpoint to any actual locations.
214
214
Target 0: (python3) stopped.
215
215
```
216
216
217
-
## Debugging in an IDE / CLion
217
+
### Debugging in an IDE / CLion
218
218
219
219
After creating a debug build with `PYTHON_DEV` enabled, you should be able to get debugging going in an IDE that support `lldb`. Below are the instructions forCLion, but you should be able to get this goingin e.g. VSCode as well.
220
220
221
-
### Configure the CMake Debug Profile
221
+
#### Configure the CMake Debug Profile
222
222
223
223
This is a prerequisite for debugging, and will enable Intellisense and clang-tidy by generating a `compile-commands.json` file so your IDE knows how to inspect the source code. It also makes sure your Python virtual env can be found by your IDE's cmake.
224
224
@@ -229,7 +229,7 @@ Under `Settings | Build, Execution, Deployment | CMake` add the following CMake
229
229
-DBUILD_PYTHON=1 -DPYTHON_DEV=1
230
230
```
231
231
232
-
### Create a run config for debugging
232
+
#### Create a run config for debugging
233
233
234
234
Under Run -> Edit Configurations... create a new CMake Application. Use the following values:
235
235
* Name: Python Debug
@@ -241,11 +241,11 @@ That should be enough: Save and close.
241
241
242
242
Now you can set a breakpoint in a C++ file. You then open your Python script in your editor and use this config to start a debug session.
243
243
244
-
## Development and Stubs
244
+
### Development and Stubs
245
245
246
246
`*.pyi` stubs in`duckdb-stubs` are manually maintained. The connection-related stubs are generated using dedicated scripts in`tools/pythonpkg/scripts/`:
247
-
-`generate_connection_stubs.py`
248
-
-`generate_connection_wrapper_stubs.py`
247
+
*`generate_connection_stubs.py`
248
+
*`generate_connection_wrapper_stubs.py`
249
249
250
250
These stubs are important forautocompletein many IDEs, as static-analysis based language servers can't introspect `duckdb`'s binary module.
251
251
@@ -256,7 +256,7 @@ python3 -m pytest tests/stubs
256
256
257
257
If you add new methods to the DuckDB Python API, you'll need to manually add corresponding type hints to the stub files.
258
258
259
-
## What are py::objects and a py::handles??
259
+
### What are py::objects and a py::handles??
260
260
261
261
These are classes provided by pybind11, the library we use to manage our interaction with the python environment.
262
262
py::handle is a direct wrapper around a raw PyObject* and does not manage any references.
@@ -268,9 +268,9 @@ I say *can* because it doesn't have to, using `py::reinterpret_borrow<py::object
268
268
269
269
When directly interacting with python functions that return a `PyObject*`, such as `PyDateTime_DATE_GET_TZINFO`, you should generally wrap the call in `py::reinterpret_steal` to take ownership of the returned object.
270
270
271
-
# Troubleshooting
271
+
## Troubleshooting
272
272
273
-
## Pip fails with `No names found, cannot describe anything`
273
+
### Pip fails with `No names found, cannot describe anything`
274
274
275
275
If you've forked DuckDB you may run into trouble when building the Python package when you haven't pulled in the tags.
276
276
@@ -286,7 +286,7 @@ git fetch --tags upstream
286
286
git push --tags
287
287
```
288
288
289
-
## Building with the httpfs extension Fails
289
+
### Building with the httpfs extension Fails
290
290
291
291
The build fails on OSX when both the [`httpfs` extension]({% link docs/stable/extensions/httpfs/overview.md %}) and the Python package are included:
292
292
@@ -300,7 +300,7 @@ make: *** [release] Error 1
300
300
301
301
Linking in the httpfs extension is problematic. Please install it at runtime, if you can.
302
302
303
-
## Importing duckdb fails with `symbol not found in flat namespace`
303
+
### Importing duckdb fails with `symbol not found in flat namespace`
... then you've probably tried to link in a problematic extension. As mentioned above: `tools/pythonpkg/duckdb_extension_config.cmake` contains the default list of extensions that are built with the python package. Any other extension might cause problems.
312
312
313
-
## Python fails with `No module named 'duckdb.duckdb'`
313
+
### Python fails with `No module named 'duckdb.duckdb'`
314
314
315
315
If you're in`tools/pythonpkg` and try to `import duckdb` you might see:
ModuleNotFoundError: No module named 'duckdb.duckdb'
325
325
```
326
326
327
-
This is because Python imported from the `duckdb` directory (i.e. `tools/pythonpkg/duckdb/`), rather than from the installed package. You should start your interpreter from a different directory instead.
327
+
This is because Python imported from the `duckdb` directory (i.e. `tools/pythonpkg/duckdb/`), rather than from the installed package. You should start your interpreter from a different directory instead.
0 commit comments