Python and SAP: Part 1 - Connecting to SAP

First things first - you want to use Python with SAP, you'll need to install some stuff.

You'll need PyRFC - a module to call RFC_READ_TABLE from SAP (the function module you need to use to read data from SAP tables)

From the PyRFC website (https://github.com/SAP/PyRFC):

The pyrfc Python package provides Python bindings for SAP NetWeaver RFC Library, for a comfortable way of calling ABAP modules from Python and Python modules from ABAP, via SAP Remote Function Call (RFC) protocol.

Here are some instructions on getting this done. I had a terrible time with this. 

Key points from my install:

  • Make sure that you install the CYTHON distribution to match the version of pyRFC that you are using (in my case it was 19.2). You can search for the distribution by modifying the following link: 

  • SAPCAR.exe is a program that you'll need to download from the SAP service place. Put it in a folder for later.

  • Download the SAR file from http://service.sap.com/rfc-library and put it in the same directory as your SAPCAR.exe

  • I had to install the GCC binaries to compile PyRFC. You may not need to do this, but this link made for the most simple install: 

  • Once you extract everything, move the NWRFSDK folder to your C path to keep things simple and then:

    • Set SAPNWRFC_HOME=%PATH%;C:\nwrfcsdk\lib

    • move the contents of C:\nwrfcsdk\include to C:\nwrfcsdk\lib\include

Once you have everything installed, the test script provided in the link above is pretty straightforward. 

import pyrfc

ASHOST='myhostserver'
CLIENT='111'
SYSNR='00'
USER='user'
PASSWD='password'

conn = pyrfc.Connection(ashost=ASHOST, sysnr=SYSNR, client=CLIENT, user=USER, passwd=PASSWD)

If you get errors, check that you've followed everything in the link above to the letter AND make sure you have set your path variable correctly. 

The setup process is a complete pain, but totally worth it. Post a comment below if you have any questions. I'll try to help where I can.

- ab

Alexander BakerComment