GT.M binding to Python

GT.M binding to Python


M is both a language and a database. Just like with most other popular NoSQL databases it is useful to be able to access M from many different languages, even though using it from the M language might be the preferred approach. Bindings to other languages make the M database available to a larger number of applications and facilitates the growth of a larger community.


A Python module has now been created to make possible to use GT.M from python scripts.

This Python module builds upon the gtm_cip C-API of GT.M.

It wraps the C API into a C++ class, that in turn is wrapped with SWIG for Python.


Details of this Python binding for GT.M are available in this article in the OSEHRA Technical Journal.


The Python module is called “gtm” and has been uploaded to pypi at,


Making it possible to install it using the “easy_install” command as:


easy_install   gtm


Note that this install only the GT.M bindings for Python.
It is expected that GT.M will already be installed in the target system.


After installing the gtm python module, you still have to proceed with the two following post-installation steps


A) Set the GTMCI environment variable to the full path of the file. Typically, this file will go to your python installation after you call easy_install. In our particular case, that location is:


The location in your system, of course, could vary.


B) Copy the _gtmaccess.m file in to one of the directories where you have M routines. These directories are the ones that you already include in the gtmroutines environment variable. The _gtmaccess.m file is placed by easy_install in the directory:


C) Add to your LD_LIBRARY_PATH environment variable the directory where you installed GT.M.

More precisely, the directory where the GT.M library


is currently located. This will make possible for the Python binding to find the GT.M shared library when you run python scripts. This directory is what you would typically have in the "gtm_dist" environment variable.



Once you have completed the post-installation, you can try some of the tests available at


For example, start python code such as:


from gtm import GTM

db = GTM()

print db.version()

print db.about()



Then follow with more interesting code, such as:


from gtm import GTM

db = GTM()

globalName = '^Capital("US")'

setValue = 'Washington'

db.set( globalName, setValue )

globalName = '^Capital("UK")'

setValue = 'London'

db.set( globalName, setValue )

getValue = db.order( globalName )

print "Order of ", globalName, " = ", getValue

getValue = db.query( globalName )

print "Query of ", globalName, " = ", getValue

db.execute( 'write $ZVERSION,!')




Enjoy GT.M from Python !




GT.M binding to Python

Sam Habiel's picture

Bravo Bravo Bravo! This so far seems to be the best integration effort for M with other languages (certainly the one with the best license). Sam


Measuring Python binding performance

Luis Ibanez's picture

For an quick evaluation of comparative performance,
Please see:


GT.M binding to Python

Erik Zoltan's picture

This is a great article but it is out of date and some of the links and github resources no longer work. I wonder if anyone is actively using GTM or YottaDB from Python, and if there are resources with OSEHRA on this subject?


Functionality should still work; updated links

K.S. Bhaskar's picture

Although the article is out of date, the functionality described should still work as documented on the YottaDB/GT.M side; I don't know about on the Python side. With regard to links, for YottaDB user documentation, please go to, and get software at

Although the content is still valid (calling from Python into an API created in M on YottaDB/GT.M), for someone who wants to access the data management capabilities of YottaDB from Python (as opposed to calling an API for a pre-existing application like VistA/Fileman), I would recommend calling the C Simple API ( from Python. The YottaDB development team intends to create a standard Python wrapper in the future, as resources permit.

– Bhaskar