New York kdb Training 2019
Spaces still available. Register now to learn Kdb+.

kdb Python API - qPy PyQ

There are three libraries available that allow you to combine Python and Kdb: Daniel Nugent's Q, qpy and PyQ.

Python Libraries Available

Library Website Description Python Versions Supported OS's Supported
Dan Nugents Python Library site Python interface for querying kdb servers, successor to qpy 2.4 2.7 3.3 Linux Mac Win
qpy site First Python interface for querying kdb servers, modeled after the Java interface. 2.7 3.3 Linux Mac Win
PyQ site Brings Python and Q interpreters in the same process and allows code written in either of the languages to operate on the same data. 2.7 3.3 Linux Only
*(other platforms require compiling)

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

  1. 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:
  2. In another terminal window, download the files to your working directory, and unzip. Then run kdb.
  3. In Python, import the module, connect and send query:
  4. 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

  1. 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:
  2. 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:
  3. Download kdb.py from https://bitbucket.org/halotis/qpy/src/, qpy's SVN, and move it to your relevant Python folder
  4. Start your Q instance and open it up to an appropriate port number, such as 5001
  5. Back in the Python instance:
  6. 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
  7. 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.