PyRFC — SAP

Jagadish Songapa Gounder
4 min readSep 5, 2018

In two previous post, we have seen how to call BAPIs from Java (see BAPI and Java 1 and SAP and BAPI 2). Let’s see how to use the Python (IPython in particular) for interactive queries SAP BAPIs.

First, you need libraries to download SAP NW RFC SDK libraries. For this purpose, unfortunately, you need to have access to marketplace (see: PyRFC github). If you are a student of WSB, please approach one of your SAP instructors. You will also get access to a NWRFC programming guide. It might be helpful.

We need a Linux instance (I tested it on Ubuntu). The ubuntu would be a great start. The NWRFC libraries are also available for Windows. MacOS owners need to use a virtualized linux instance with virtualbox or docker (see at the end of the blog post).

Linux preparation
On your fresh copy of Ubunut 16:10, you need to install pip and git (You can also use Docker with this Dockerfile):

sudo apt-get update

# git for cloning the PyRFC source code
sudo apt-get install git -qq

# pip for installing PyRFC
sudo apt-get install python3-pip -qq

# ensure we have the newest pip version
sudo pip3 install -U pip

# ipython is best for running python interactive
sudo pip3 install ipython
Install PyRFC
When we have the required packages installed, we can proceed with the SAP Python wrapper for NWRFC library. It is open source and you can grab its copy from PyRFC github.

mkdir tmp;
cd tmp;
git clone https://github.com/SAP/PyRFC.git;
cd PyRFC/dist
pip3 install pyrfc-1.9.5-cp35-cp35m-linux_x86_64.whl
It is just wrapper that let’s you to communicate with NWRFC libraries from your python program.

Install nwrfcsdk
At this point, we just need to install NWRFC SDK and inform the operation system where to find it. The file will an zip or tar.gz archive: *nwrfcsdk_x64_linux.zip* for Linux and *nwrfcsdk_x64_win.zip* for Windows.

# ubuntu usually does not come with unzip installed
sudo apt-get install unzip -qq
sudo unzip /tmp/nwrfcsdk_x64_linux.zip

# move in the proper location
sudo mv nwrfcsdk /usr/sap/nwrfcsdk
When you run ls /usr/sap/nwrfcsdk in bash, you should see the following directories:

bin
demo
doc
include
lib
Now, you need to inform the Linux where to find the nwrfc libraries:

sudo su
echo “# include nwrfcsdk” > /etc/ld.so.conf.d/nwrfcsdk.conf && \
echo “/usr/sap/nwrfcsdk/lib” >> /etc/ld.so.conf.d/nwrfcsdk.conf ;
# see https://linux.die.net/man/8/ldconfig
ldconfig
Test the setup
Run the ipython or python shell and check whether importing pyrfc libaries works for you:

root@38c1c1fe33fb:/# ipython
Python 3.5.2+ (default, Sep 22 2016, 12:18:14)
Type “copyright”, “credits” or “license” for more information.

In [1]: from pyrfc import *
If it does not work, you might need to

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/sap/nwrfcsdk/lib
first. Evghenii Kunitski, thanks for spotting it.

If you see no errors, congratulations. Everything works.

Call BAPIs using PyRFC
Open the IPython shell (Notice, the next steps will assume that we work in the same IPython shell session):

from pyrfc import Connection

user = ‘wb’
passwd = ‘wbsecretpass’
saprouter = ‘/H/saprouter.myhost/S/3297’

conn = Connection(user=user, passwd=password,
mshost=’i78z’,
msserv=’3678',
sysid=’1',
group=”SPACE”,
saprouter=saprouter,
client=‘900’)
Having a connection to SAP system, let’s call BAPI to get user details (in my case: wb):

b_result = conn.call(‘BAPI_USER_GET_DETAIL’,
USERNAME = ‘wb’,
CACHE_RESULTS = ‘ ‘)
iPython is great when you need to discover and explore BAPI results (without going to SAPGUI and BAPI transaction). Just type b_result and hit enter:

In [3]: b_result
# you will see what the b_result contains
Go back to your opened IPython shell and explore the results (after every line, hit enter):

b_result[‘ADDRESS’]

b_result[‘ADDRESS’][‘LASTNAME’]

b_result[‘ADDRESS’][‘LASTNAME’].encode(‘utf8’)

print b_result[‘ADDRESS’][‘LASTNAME’].encode(‘utf8’)
Exploring BAPIs using PyRFC
You can also learn more about BAPIs, you did not know before. Let’s show it taking BAPI_USER_CHANGE as an example. Call in your opened IPython shell (notice: it will fail):

r2 = conn.call(‘BAPI_USER_CHANGE’)
To help comes get_function_description (see get_function_description docs). This function with the dir command will help us to understand the interface of BAPI_USER_CHANGE.

ds = conn.get_function_description(‘BAPI_USER_CHANGE’)
# dir will show us the structure of the variable
dir(ds)
# parameters sounds promising
dir(ds.parameters)
# let’s print our all the parameters required for our BAPI
for p in ds.parameters: print p
Let’s print out the input parameters:

for p in ds.parameters:
if p[‘direction’] == ‘RFC_IMPORT’:
print p[‘name’]
Having enough understanding how BAPI_USER_CHANGE works. We can change the city field for our user:

# let’s use the ADDRESS from our previous BAPI call
updated_address = b_result[‘ADDRESS’]
updated_address[‘CITY’] = u’Wroclaw’
r3 = conn.call(‘BAPI_USER_CHANGE’,
USERNAME=’wb’,
ADDRESS=updated_address)

# let’s see whether the change was successful
r3
As your homework, please use IPython and PyRFC to call the following BAPIs:

BAPI_PO_GETDETAIL
BAPI_MATERIAL_GET_DETAIL
BAPI_VENDOR_GETDETAIL
Use a random Purchase Order (ME2N), Material, or Vendor from your SAP system.

References
how to download SAP NW RFC SDK: http://sap.github.io/PyRFC/install.html
how to install SAP NW RFC SDK: http://sap.github.io/PyRFC/install.html#install-c-connector
how to read a table — http://www.alexbaker.me/code/python-and-sap-part-2-getting-data-from-sap

--

--

Jagadish Songapa Gounder

Aspiring data science enthusiast who is passionate about transforming data into useful products and Full Stack Developer