View as source file or XML.

Zorba with Python - Windows Installation

These steps were checked on Windows 7 and Python 2.7 (32bits) with the Zorba Revision 10986 from Jun 29th, 2011, this revision has important bug fixing and it requires at least this version to work.It should work fine with any Python 2.7.X (32 bits) version.

Install Python

Download and install Python binaries from http://www.python.org/getit/. If you want to debug the python binding you will need to download the sources and compile python in order to get the debug library of python: python_d.lib.

Install Swig

Zorba uses Swig to create the binding in python language, you only need the binary to create it, download from http://www.swig.org/download.html and install.

Installing Zorba

Get Zorba sources and follow the Zorba Build Instructions.

Compiling the Zorba Python Library

In order to compile the Python binding you need first to follow the steps to compile Zorba, but before compiling, you must need to add one extra variable to the CMAKE command line:-D ZORBA_USE_SWIG=ON Turn on the Swig library, in case you have other bindings, this flag will help to activate them.You should not need any other extra flag, cmake should be able to detect python at this point, but in case cmake can't find python installation you can add these two flags:-D PYTHON_LIBRARIES=... Specify where the libs directory is located, i.e. "C:\Python27\libs"-D PYTHON_INCLUDE_PATH=... Specify where the include directory is located, i.e. "C:\Python27\include".After adding those lines CMAKE will add automatically the Python binding project and after compiling you will get two files: _zorba_api.pyd and zorba_api.py, these files are the binding for zorba.

Testing

Verify that Zorba is working by command line:
C:\>zorba.exe -q '2+1'
<?xml version="1.0" encoding="UTF-8"?>
3
Test the following example in python, this example contains a direct path to the two files generated, you can change this according to your configuration or by moving those files to libs directory:
import sys
sys.path.insert(0, 'C:\\zorba\\vs2010\\swig\\python')
import zorba_api

def example1(zorba):
  xquery = zorba.compileQuery("1+2")
  print xquery.printPlanAsXML()
  print xquery.execute()
  return
def example2(zorba):
  xquery = zorba.compileQuery("(1,2,3,4,5)")
  iter = xquery.iterator()
  iter.open()
  item = zorba_api.Item_createEmptyItem()
  while iter.next(item):
    print item.getStringValue()
  iter.close()
  iter.destroy()
  return
  
def example3(zorba):
  try:
    xquery = zorba.compileQuery("1 div 0")
    print xquery.execute()
  except RuntimeError, e:
    print e
  return

def example4(zorba):
  try:
    xquery = zorba.compileQuery("for $i in (1,2,")
    print xquery.execute()
  except RuntimeError, e:
    print e
  return

store = zorba_api.InMemoryStore_getInstance()
zorba = zorba_api.Zorba_getInstance(store)

print "Example1:"
example1(zorba)
print ""

print "Example2:"
 example2(zorba)

print ""

print "Example3:"
 example3(zorba)

print ""

print "Example4:"
 example4(zorba)

print ""

zorba.shutdown()
zorba_api.InMemoryStore_shutdown(store)
Useful links Zorba Python API Documentation