It’s been a while since I wrote my last Python blog…July 15, 2011 Tasting the mix of Python and SAP – Volume 3…almost a year…so I thought it was a good thing to get back into action…but…what else could I wrote about? I started to check my options and of course I quickly thought on SAP HANA. After all, I have my own server running on Amazon Web Services so it sound like a good plan.

At first I wanted to use SAP HANA as an ODBC connection like I did with #R in the early days…but then I heard that Python was actually embedded on the SAP HANA Client installation, so it only took a little checking, and some SAP internal forum to discover that, using Python and SAP HANA is easier that I could ever imagine…

First, you need to go where your SAP HANA Client is installed, and then copy the 3 files from the hdbcli folder…


Then, go to the Python folder and copy these files into the Lib folder…


Do the same with this 2 files in the hdbclient folder, copy them and paste them into the Python/Lib folder…


With that, we’re ready to go…download any Python IDE and assign Python.exe (from the Python folder on hdbclient) as the Python executable.

import dbapi

conn = dbapi.connect(‘’,

     30015, ‘SYSTEM’, ‘manager’)

print conn.isconnected()

If this little program prints “TRUE” on the screen, it means that the connection is done.

Go to and copy the file into your Python folder. Executed in the command line like Python to install the tools and then add the directory “full path/Python/Scripts” to your path variable in the Windows System variables…

With that, you can directly call Easy_Install…that we’re going to need in order to install Bottle, a Python Web Micro framework.

In the command prompt write:

easy_install -U bottle

With that, we’re ready to start 🙂

from bottle import get, post, request, run, redirect, route

import dbapi

import time


def login_form():

    return ”'<DIV ALIGN=’CENTER’><BR><BR><BR><BR>

                <H1>Python (Bottle) & SAP HANA</H1>

                <BR><TABLE BORDER=’1′ BORDERCOLOR=’BLUE’


                <FORM METHOD=’POST’>


                <INPUT TYPE=’TEXT’ NAME=’Server’></TD></TR>


                <INPUT TYPE=’TEXT’ NAME=’Port’></TD></TR>


                <INPUT TYPE=’TEXT’ NAME=’User’></TD></TR>


                <TD><INPUT TYPE=’PASSWORD’ NAME=’Passwd’></TD></TR>

                <TR><TD COLSPAN=’2′ ALIGN=’CENTER’>

                <INPUT TYPE=’SUBMIT’ value=’Log In’ NAME=’LOG_IN’>

                <INPUT TYPE=’RESET’ value=’Clear’></TD></TR>





def login_submit():

    global cur

    Server = request.forms.get(‘Server’)

    Port = request.forms.get(‘Port’)

    User = request.forms.get(‘User’)

    Passwd = request.forms.get(‘Passwd’)

    Port = int(Port)

    conn = dbapi.connect(Server, Port, User, Passwd)

    cur = conn.cursor()



def choose_parameters():

    global cur


    ret = cur.execute(query)

    ret = cur.fetchall()

    output = “‘<CENTER><FORM METHOD=’POST’>”

    output += “Carrier <SELECT NAME=’Carrid’>”

    for row in ret:

        carrid = str(row[0])

        carrname = str(row[1])

        output += “<OPTION VALUE=’%s’>%s</OPTION>” % (carrid, carrname)

    output += “</SELECT>”


    ret = cur.execute(query)

    ret = cur.fetchall()

    output += “City From<SELECT NAME=’Cityfrom’>”

    for row in ret:

        cityfrom = str(row[0])

        output += “<OPTION VALUE=’%s’>%s</OPTION>” % (cityfrom, cityfrom)

    output += “</SELECT>”

    output += “<INPUT TYPE=’SUBMIT’ value=’Show Query’ NAME=’show_query’>”

    output += ” </FORM></CENTER>”

    return output


def show_query():

    counter = 0

    start = time.clock()

    carrid = request.forms.get(‘Carrid’)

    cityfrom = request.forms.get(‘Cityfrom’)




                WHERE SBOOK.CARRID = ‘%s’ AND CITYFROM = ‘%s’

                AND PASSNAME <> ”

                AND SBOOK.MANDT = 300

                AND year(FLDATE) = 2012

                ORDER BY FLDATE DESC”’ % (carrid, cityfrom)

    ret = cur.execute(query)

    ret = cur.fetchall()

    output = “<DIV ALIGN=’CENTER’><TABLE BORDER=’1′>”

    output += “<TR BGCOLOR=’#B9C9FE’>”

    output += “<TH>Carrier</TH><TH>Connection</TH>”

    output += “<TH>Flight Date</TH><TH>Passenger Name</TH>”

    output += “<TH>City From</TH><TH>City To</TH>”

    output += “</TR>”

    for row in ret:

        counter += 1

        carrid = str(row[0])

        connid = str(row[1])

        fldate = str(row[2])

        passname = row[3].encode(‘utf-8’)

        cityfrom = row[4].encode(‘utf-8’)

        cityto = row[5].encode(‘utf-8’)

        output += “<TR BGCOLOR=’#E8EDFF’>”

        output += ”'<TD>%s</TD><TD>%s</TD>


                         <TD>%s</TD><TD>%s</TD>”’ % (carrid, connid, fldate, passname, cityfrom, cityto)

        output += “</TR>”

    output += “</TABLE>”

    end = time.clock()

    time_taken = end – start

    output += “<H1>%s records in %s seconds</H1></DIV>” % (counter, time_taken)

    return output

run(host=’localhost’, port=8080)

We when run this code, the Bottle Web Server is going to start, so we need to go to http://localhost:8080/login to start the application.


We need of course to log in 😉



We can choose a Carrier and the City From for our query (Of course, both Dropdown list are filled with data coming from SAP HANA).


Let’s show the query in a nice table…


It took a little bit more than 3 seconds to fetch data from SAP HANA and the to print it on Python. Almost 3K records in 3 seconds? That’s fast…specially when we considered that Python is not exactly the fastest tool in the market 🙂

I hope you like this blog, as I really enjoyed working on it. It’s always a great experience to use Python, SAP HANA or any other programming language to develop something new 😘

To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Vasanth S

    Using Python is really good one. Than compare to JAVA or Other OOPS, More and More features available in Python language.

    Great Blog..!

    Vasanth S

  2. Tobias Oberstein

    I have 2 questions:

    Does that use ODBC under the hood or is the a native HANA client implementation for Python?

    Is that available for Linux 32 Bit?

    Thanks, Tobais

    1. Alvaro Tejada Galindo Post author

      Dear Tobias:

      I have no clue…I don’t have access to the source code so there’s no way for me to know…but my guess would be native implementation…

      I’m not a Linux user, but I guess the libraries should be included as well…



  3. Ingo Sauerzapf

    Hi Blag,

    I had to try this out with a Windows 7 64Bit and Python 2.7.2 that was already installed on my machine. For my environment it did not work. I assume it is the python version. I did install the client tools for Rev. 35 and copied the files over but when trying to import the dbapi I get a “No module named dbapi” error.

    I noticed that there is a pythong version coming with the Rev. 35 client tools and thought that everthing would have been set up there already, but I was wrong. However when copying the entire hdbcli folder under the .\Lib directory including the folder as suggested by the general package concept of pythong and copying the two pyhdbcli.* files directly into the .\Lib folder I could actually do an import with

    from hdbcli import dbapi

    I went back to the pythong 2.7.2 doing the copy of the entire folder hdbcli and there i now get a “DLL load failed: The specified module could not be found”

    Not sure it this is really the phython version I am using but for me it looks like the hana package is quite picky in that respect.



    1. Alvaro Tejada Galindo Post author

      Hey Ingo 🙂

      You’re right…I test this on my Python 2.7.3 installation…and got the same error…my assumption is that they build the module in a way that can be only used by the Python version bundled with the SAP HANA Client…some like…”If is not version X.X.X then don’t do anything”…anyway…I’m might do more research today to see if there’s anything else missing and will let you know…



      1. Martin Boissier


        even though it’s more a work around, than a fix, I did the following:

        – add to your PYTHONPATH the directory of the hdbclient (e.g. “C:\Program Files\sap\hdbclient\;”, Google to see how to edit PYTHONPATH in your OS)

        – wherever your .py script is, you have to use the shipped Python. So write a little script, e.g. run.bat:”C:\Program Files\sap\hdbclient\Python\python.exe”

        – to run your script, don’t call or doubleclick the script itself, but use the run.bat to use the correct python interpreter

        Hope that helped.

        EDIT: you might need to close and restart the commandline until the path changes are recognized.

        1. David Makovoz

          I have the same problem. It works if I call python26.exe that comes with Hana.

          So I create a file and copied the three files (, etc.) to a dbapi folder.

          And then ran python install.


               import dbapi
          works in my IDE (I use Spyder). I.e. import doesn’t throw an error,

          However, dbapi.connect() does:

               AttributeError: ‘module’ object has no attribute ‘connect’

          What am I doing wrong?



    1. Alvaro Tejada Galindo Post author


      The Python DBAPI is not released for customer, so it might not be included in newer releases. Anyway, you should be able to find it on Program Files –> sap –> hdbclient.

      I have only test this on Windows…not sure if it’s included or not on Linux.



    1. Alvaro Tejada Galindo Post author


      You can’t find python.exe? Now that’s weird…maybe you need to re-install the SAP HANA Client…if you have a Python folder…then python.exe should be there for sure…and nope…as far as I know…only Python 2.6.4 works with SAP HANA…



      1. Christoph Schuffenhauer

        Thanks Blag,

        I checked by installing the hdbclient again as a separate installation. And it’s there. So, no idea, why it is missing in the original installation.



  4. Inna Nickel

    Hi Alvaro,

    I’m using Python 3.3. While running I get the following message:

    Traceback (most recent call last):

      File “C:\Users\…\HANA\”, line 1, in <module>

        import dbapi

      File “C:\Python33\lib\”, line 13, in <module>

        from exceptions import StandardError

    ImportError: No module named ‘exceptions’

    What is the reason for this error?

    Kind regards,


    1. Alvaro Tejada Galindo Post author


      As far as I know…the dbapi library only works with the Python 2.6.4 that comes bundled in the SAP HANA Client installation…

      C:\Program Files\sap\hdbclient\Python

      Using outside that environment will led you to some errors…



      1. Madhu Kolli

        Hi Blag – This is an awesome blog.I was wondering if I can get your thoughts on how we could connect Applications with Perl language to SAP BW on HANA.

        I would appreciate if I could get some leads on the same.

        1. Alvaro Tejada Galindo Post author


          So far…you have two options…

          a) Use and ODBC connection

          b) Build your own library for native connection

          I have tried the second option…but my C/C++ skills doesn’t go that far…also…I’m not really familiar with Perl…so the first option would be best to go…



          Developer Experience.

  5. Limor Erez

    The Python that installed with SAP HANA Client doesn’t include Lib/unittest libraries. my Python project inherits from the unittest.TestCase class. so after moving to the python executable provided by SAP HANA Client I could not run my tests. Any idea how to fix that ?

    1. Alvaro Tejada Galindo Post author


      You can install Easy_Install on the SAP HANA’s bundled Python and then install the libraries you need…that’s just what I did to have Bottle enabled 🙂



      Development Culture.

    1. Alvaro Tejada Galindo Post author


      I’m not aware of such functionality…my according to my testing…there’s not…the only way is to provide ID/PWD…keeping in mind that the dbapi is kind of “experimental” and not meant to be use in application development…



      Development Culture.

  6. Deepti Lakshmi

    Hi ,

    I am using HANA client 81 . I could not find the folder hdbcli at all to copy the files. Is there some thing missing? Neither i could find the python folder

    Deepti lakshmi.A

    1. Alvaro Tejada Galindo Post author


      As I said on my blog, the Python connector for SAP HANA is not supposed to be use in production environments…so there’s no guarantee that it will make to greater SAP HANA versions…I haven’t upgrade to client 81 so I can’t really tell if it’s there or not…



      Development Culture.


Leave a Reply