Making Python Talk to SAP

SAP provides an open source python library called pyrfc that can be used to make RFC calls to SAP and use various functions (apparently you can even called python scripts from SAP see this, but that's probably something for another day). I'm not an ABAP developer so my SAP knowledge/terminology use may not be a 100% accurate. But I can get the job done.

My use case is to pull information from SAP tables. I had built a small utility to be able to pull some table data from SAP to Python but never made more progress on that. Today, I have a need to use this library again but setting it up was a bit of a pain. Hence, I've decided to document the process. Note that the github repository readme is pretty clear. But the process of setting up the library in your environment is something thats not explained really well and it took me the usual trial and error approach to get it working. It assumes that readers are well versed with how libraries in Linux work and how they are configured. But for folks like us, especially people who haven't had too much development experience in C and Linux, it can trip us up.

Not that the nwrfcsdk is something that is not freely and openly available, you need to have the right access to SAP Support portal where you can download it. Obviously your organization needs to be a SAP customer to use it.

This post provides details on how the pyrfc library can be setup in a Linux environment. When downloading the nwrfcsdk, ensure that you download the right version.

The Linux 64 bit version of the SDK ends with a prefix 52, the Windows 64 bit version ends with 55

Now for setting this up on Linux. You might notice a simple pip install pyrfc command might end up in errors. To ensure that doesn't happen, ensure that the following conditions are met.

#Run pip install pyrfc under the root user context.
sudo -s

#Update your local package definitions
apt update

#Install the tools to compile C for python as well as pip, if you don't have it already
apt install build-essential python3-dev python3-pip

#Place the downloaded nwrfcsdk files and reference the environment variable to the root directory of the downloaded files. For added clarity, this directory referenced below should have subdirectories /lib and /include

export SAPNWRFC_HOME=/home/nwrfcsdk_linux/

#Install python wheel package
pip install wheel

#Finally install pyrfc package
pip install pyrfc

If you did everything alright, you should see the following glorious view on your terminal:

Now you may encounter the following error when you attempt to import the library in Python as I did:

This means the shared library in linux hasn't been referenced properly. To do that, I found this helpful link but it didn't entirely resolve my issue. Reading more about ldconfig allowed me to fix it by simply running ldconfig -v.

Then you can confirm the libraries were registered correctly via ldconfig -p | grep sap

ldconfig -p | grep sap

Additional Reference Resources

Shuaib Mohammad

Shuaib Mohammad