kdb Python API - qPy PyQ
Article updated 2024-08-27.
Kola was released 2023 and is the fastest most modern kdb+ python API.
I would strong recommend Kola as your chosen API unless you have a strong reason to choose an alternative.
Article updated 2023-05-15. You now have two main options:
- qPython3 - Is an API for querying or uploading data to/from kdb and python.
This is an open source project now under FINOS. - PyKX - Is a closed source library allowing you to run python and kdb in the same memory space.
Previously kx purchased the open source PyQ but now only continues PyKX development commercially
Python Libraries Available
Library | License | Last Updated | Website | Description | Python Versions Supported |
---|---|---|---|---|---|
Kola | Open Source | 2023 | Site | Python Polars Interface to kdb+/q written in rust language, is released to Linux/macOS/Windows.
Authored by Jo Shinonome. More info.. |
3.8 to 3.12 |
qPython3 | Open - Apache2 | 2023 | site | qPython is a Python library providing support for interprocess communication between Python and kdb+ processes
Synchronous and asynchronous queries, kdb+ v4.0 |
2.7 3.6 3.9 3.10* and numpy 1.8+ |
PyKX | Closed / Commercial | 2023 | site | Brings Python and Q interpreters in the same process
allows code written in either of the languages to operate on the same data. Seems to be kx's successor to PyQ, however where pyq was apache license, PyKX is commerical. |
2.7 3.9 3.10* |
PyQ | Open - Apache2 | 2020 | site | Brings Python and Q interpreters in the same process
allows code written in either of the languages to operate on the same data. |
2.7 3.9 3.10* |
Python Library |
Open | 2013 | site | Python interface for querying kdb servers, successor to qpy.
Not updated in 10 years. |
2.4 2.7 |
Open | 2010 | site | First Python interface for querying kdb servers, modeled after the Java interface. | 2.7 |
Dan Nugents Python Library
Dans Python Library can be downloaded here.
Successor to qpy, this library is based heavily on it. It is quicker than qpy
however you are unable to easily display tables. Daniel is no longer actively maintaining it but does accept bug fixes, there are no plans to support v3.x of Python.
2017 Update from Dan
For anyone else who needs a Python library, I am highly recommending the exxeleron qpython library (though it does require numpy, which requires 2.6 As a minimum, I believe, which can be a limitation)
Example of Dans kdb+ Python API
- Create a folder in your Python working directory so that you can separate modules from other files. You can find out Python's working directory by:
- In another terminal window, download the files to your working directory, and unzip. Then run kdb.
- In Python, import the module, connect and send query:
- Example of sending / receiving data and tables.
qpy
qpy can be downloaded here.
Originally developed back in 2009 qpy is easy to setup and contained within one Python module.
You can display tables in the Python command line with ease however it is no longer under development and can be slow if displaying a lot of data.
qpy's successor is Daniel Nugent's Q.
Example of qpy kdb+ Python API
Setup
- Create a folder in your Python working directory so that you can separate modules from other files. You can find out Python's working directory by:
- You can then create a folder if you wish (you may want to do this to separate Python modules from other files and folders): os.makedirs("pythonFolderName") Remember that in Python you must append any folders you wish to access to Python's list of paths:
- Download kdb.py from https://bitbucket.org/halotis/qpy/src/, qpy's SVN, and move it to your relevant Python folder
- Start your Q instance and open it up to an appropriate port number, such as 5001
- Back in the Python instance:
- Receiving and Sending Data To a Q Instance
You will need to use the print command some of the time:
print conn.k('select from table') #for example
when receiving tables - You can send data by running a query:
Recommendations
When accessing a couple of thousands of rows it can be quite slow, if that is a common occurrence you may wish to use another Python interface. However, in general it is best to avoid using Python and write your functions in Q, only later transferring the specific data to Python.
PyQ
Loads python as a dynamic library inside of kdb, allows entering code in either language using shared memory and data.
As recommended on Kx's Wiki page. If you are using a non Linux platform, it requires more effort to get up and running as you will need to compile it for a specific platform. PyQ can pre-compile Q queries and reuse them to speed up processing. But it is still recommended to minimise the computing done in Python, regardless of library being used, as Python is consierably slower than Q.
Install instructions are available here.