/wp-content/uploads/2014/08/python_logo_511224.png

Python is an easy to learn programming language. As the documentation on the official Python page says, “Python can be easy to pick up whether you’re a first time programmer or you’re experienced with other languages. Python is powerful… and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open”.


The PyRFC module is the newest way to get externally running Python code integrated with SAP.  Srdjan Boskovic explains nicely why you would want a Python/ABAP stack. As he says, “Our language of choice for the web development is Python and a frequent question we hear is why we are using Python and not Java, the de-facto standard for the enterprise.”


Setup and installation of PyRFC isn’t too difficult on GNU/Linux and OS X. On Windows, it can be a little painful for Python noobies as the C-extension needs to be compiled, and by default Windows doesn’t ship with a compiler. These instructions are geared more towards beginners and get you started in the barest possible time without having to compile, as I’ve pre-compiled the module for you. I’ve tested this process on Windows 7 and 8.


Quick setup process:


  • Install Python 2 32-bit version. Get Python from https://www.python.org/downloads/windows/ and choose to download for Windows. Then select “Latest Python 2 Release”, which at the time of writing this was 2.7.8. I choose the “Windows x86 MSI Installer” file. When I install, I prefer to install “just for me”, and I choose the option to add python.exe to Path
  • You’ll need SAP’s C-connector, NWRFC. And in order to get that you’ll also need an “S-number” (SAP logon) that allows you to download software from the SAP support website. Here is a PDF telling you how to get the files you need from the SAP support site.


  • Extract the C-connector. Here’s how: open a cmd shell (or PowerShell) and do: 


SAPCAR_315-20010451.EXE -xvf NWRFC_24-20004566

(Note that I used the Windows/IA32 versions of these files, and that the version numbers of the files that you download might not be the same, adjust accordingly!)


  • “Install” the C-connector. Put the “nwrfcsdk” folder that the above step created where you want it installed. For this exercise, I moved it to be under C:\Python27\


It will be necessary to adjust your environmental variables for the install location. Open environmental variables editor, which can be opened as follows: open Control Panel, go to System and Security, then  “System”, “Advanced System Settings”, then click the “Environmental Variables” button. Edit the “Path” environmental variable. This should already have C:\Python\27\ added to it, but we need to also add:

;C:\Python27\nwrfcsdk\lib

to the end of the list. Obviously put another path in if your path isn’t C:\Python27\nwrfcsdk\lib


If you forget to set the Path, then your Python code won’t be able to use the C-connector and you’ll get an error message: “ImportError: DLL load failed”


  • We need setuptools so that we have easy_install. Download distribute_setup.py. From a cmd prompt install it like so:


python distribute_setup.py


  • And now for our Trademarked “Secret Sauce” which is why you’re reading this article! Use easy_install to install this prebuilt binary Python .egg file for PyRFC (md5 846946fe9c8596d186dbadb9258eca66).

From a cmd prompt install as follows:

C:\Python27\Scripts\easy_install-2.7.exe pyrfc-1.9.4-py2.7-win32.egg
  • Finally, we can test it:


Create a config file, sapnwrfc.cfg and insert content like this, obviously set the correct ashost (sap system), client, sysnr, user, passwd:



[connection]
# sap system ip
ashost = xxxxxxx
# sap client id
client = 001
# sap router string, optional, format like /H/x.x.x.x/...
saprouter =
# sap system number
sysnr = 00
# sap username
user = DEVELOPER
# sap password
passwd = xxxxxxx2
[gateway]
# gateway host name
gwhost =
# gateway server name
ghserv =
# name under which the python connector will register itself
program_id = PYRFC_SERVER1
# sap router string, optional
saprouter =



Create a test program file “get_system_info.py” and paste the following into it:

#!/usr/bin/env python
from pyrfc import Connection, ABAPApplicationError, ABAPRuntimeError, LogonError, CommunicationError
from ConfigParser import ConfigParser
from pprint import PrettyPrinter

def main():
   
    try:

        config = ConfigParser()
        config.read('sapnwrfc.cfg')
        params_connection = config._sections['connection']
        conn = Connection(**params_connection)

        result = conn.call('RFC_SYSTEM_INFO')

        pp = PrettyPrinter(indent=4)
        pp.pprint(result)

    except CommunicationError:
        print u"Could not connect to server."
        raise
    except LogonError:
        print u"Could not log in. Wrong credentials?"
        raise
    except (ABAPApplicationError, ABAPRuntimeError):
        print u"An error occurred."
        raise

if __name__ == '__main__':
    main()


Now, if you run:



 python get_system_info.py









You should get a response with system information, like this:


{   u'CURRENT_RESOURCES': 7,
    u'MAXIMAL_RESOURCES': 9,
    u'RECOMMENDED_DELAY': 0,
    u'RFCSI_EXPORT': {   u'RFCCHARTYP': u'4103',
                         u'RFCDATABS': u'NPL',
                         u'RFCDAYST': u'',
                         u'RFCDBHOST': u'WDFLBMD6865',
                         u'RFCDBSYS': u'ADABAS D',
                         u'RFCDEST': u'wdflbmd6865_NPL_00',
                         u'RFCFLOTYP': u'IE3',
                         u'RFCHOST': u'wdflbmd6',
                         u'RFCHOST2': u'wdflbmd6865',
                         u'RFCINTTYP': u'LIT',
                         u'RFCIPADDR': u'172.31.xx.xxx',
                         u'RFCIPV6ADDR': u'172.31.xx.xxx',
                         u'RFCKERNRL': u'740',
                         u'RFCMACH': u'  390',
                         u'RFCOPSYS': u'Linux',
                         u'RFCPROTO': u'011',
                         u'RFCSAPRL': u'740',
                         u'RFCSI_RESV': u'',
                         u'RFCSYSID': u'NPL',
                         u'RFCTZONE': u'     0'}}


It works! And how easy was that?


Notes:

  1. I’ve made some assumptions in this document. One assumption is that you’re wanting the 32-bit version of the software (this will still work on 64-bit systems!)
  2. You really should investigate using pip for installing further Python packages. For installing pip on windows see this link
  3. …and you owe it to yourself to install virtualenv which allows you to install Python modules independently from the global Python install! See the same link as above.
  4. Here is a binary egg file for python-sapnwrfc (md5 358ca360a290e52c5a55d90fb3998abb) (This file is Piers Harding’s wrapper module which was an earlier module that we used before PyRFC came along, in case you’d rather use his module instead of PyRFC).
To report this post you need to login first.

2 Comments

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

  1. Sulman Mahmood Shaikh

    Hi,

    I had followed your instructions (many thanks btw) to a T and whenever i try the simple package import (from pyrfc import *) I get the error

    Traceback (most recent call last):

      File “<stdin>”, line 1, in <module>

      File “C:\Python27\lib\site-packages\pyrfc-1.9.4-py2.7-win32.egg\pyrfc\__init__

    .py”, line 22, in <module>

        from pyrfc._pyrfc import get_nwrfclib_version, Connection, TypeDescription,

    FunctionDescription, Server

    ImportError: DLL load failed: The specified module could not be found.

    I have tried this on tow machines, one at work and home since I thought the firewall at work may be having an effect, and still the same.  Also when I easy_install the .egg relevant to me it says “error: Could not find suitable distribution for Requirement.parse(‘pyrfc-1.9.4-py2.7-linux-x86-64.egg’)” – but still it installs most of the package, is there a problem with this distribution?

    I have checked the system variables a million times and have dropped the sapnwrfc folder into the path ‘C:\Python27\nwrfcsdk\lib’ and still no luck, still get the same error, any suggestions please?

    Thanks

    (0) 

Leave a Reply