Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How To Use Python Seamlessly Inside ABAP

Python is an interpreted, object-oriented, high-level programming language and it is used in many different application areas, e.g. in scientific and numeric computing. It can be very valuable and helpful to combine SAP ABAP with Python. In this blog I will describe a way to do that via the SAP GUI for Windows COM interface.

In the past I wrote different blogs how to use Python in the context of ABAP:

Here now another way. The communication between Python and ABAP runs in this case via the COM interface. Python offers via Python for Win32 (pywin32) extensions the possibility to create COM server. With ABAP and the SAP GUI for Windows is it possible to connect these COM server and to use its functions.

  1. Download Python from here and install it.
  2. Download the Python for Windows (pywin32) Extensions from here and install it.
  3. Copy the following Python program to your computer.
    # -*- coding: iso-8859-15 -*-
    class PythonDemo:
      #To create a new GUID use the commands:
      #>>> import pythoncom
      #>>> print(pythoncom.CreateGuid())
      _reg_clsid_ = "{D1B0A23F-0B6E-46D6-8880-744DBFFB86CD}"
      _reg_progid_ = "Python.Demo"
      _reg_desc_ = "Python Demo COM Server"
      _public_methods_ = ["HelloWorld", "HelloYou", "SplitString"]
      #-Function HelloWorld-------------------------------------------------
      def HelloWorld(self):
        import platform, struct
        return "Hello World from Python " + platform.python_version() + \
          " on " + platform.system() + " (" + platform.architecture()[0] + \
      #-Function HelloYou---------------------------------------------------
      def HelloYou(self, name="Anybody"):
        return "Hello " + str(name) + " from Python"
      #-Function SplitString------------------------------------------------
      def SplitString(self, val, item=None):
        import string
        if item != None:
          item = str(item)
        return val.split(item)
    #-Sub Main--------------------------------------------------------------
    def main():
      import sys, win32com.server.register
      if sys.argv[1].lower() == "--register":
        print("Registering COM server...")
      if sys.argv[1].lower() == "--unregister":
        print("Unregistering COM server...")
    if __name__=="__main__":

    This Python program contains a class with three different demo methods:
    • HelloWorld returns a string with the using Python version, platform system and architecture.
    • HelloYou returns a string with a parameter.
    • SplitString – the standard example from the help – delivers a list of single words of a string.
    Also the class contains different annotations for the COM registration.
    The main routine of the program contains the registration code.

  4. Register this Python program as COM server.
  5. Try the following VBScript to check if everything is okay.
      '-Sub Main------------------------------------------------------------
      Sub Main()
        Set PythonDemo = CreateObject("Python.Demo")
        MsgBox PythonDemo.HelloWorld()
        MsgBox PythonDemo.HelloYou("Stefan")
        response = PythonDemo.SplitString("Hello World")
        For Each item In response
          MsgBox item
      End Sub

    This VBScript call from the COM server Python.Demo, in this case the Python program, the different methods – HelloWorld, HelloYou and SplitString.

    Here the result from HelloWorld:

    Here the result from HelloYou:

    Hint: It does not matter which version of VBScript you use – x86 or x64 – it works with both.

  6. Try the followin ABAP report.
      lo_python TYPE OLE2_OBJECT,
      lv_return TYPE STRING,
      lo_list TYPE OLE2_OBJECT
    CREATE OBJECT lo_python 'Python.Demo'.
    CHECK sy-subrc = 0 AND lo_python-Handle > 0 AND lo_python-Type = 'OLE2'.
    CALL METHOD OF lo_python 'HelloWorld' = lv_return.
    WRITE: / lv_return.
    CALL METHOD OF lo_python 'HelloYou' = lv_return
        #1 = 'Stefan'.
    WRITE: / lv_return.
    "Array is not supported as a data type for OLE parameters in ABAP
    CALL METHOD OF lo_python 'SplitString' = lo_list
        #1 = 'Hello World'.

    The ABAP report do exactly the same as the VBScript.

    Here the result of the ABAP report.

    Hint: It is not possible to use a COM method which delivers a variant from the type array. So it is not possible to use the method SplitString – SAP note 633105.


At this point we see that is easy possible to use Python inside ABAP seamlessly via COM interface. We register the Python class as COM server and use it inside ABAP.

A huge advantage is that you can change the Python class on the fly. This means you can add or change methods without a new registration. So you can develop and test your Python class independently from your ABAP code, but you can use each change immediately in your ABAP code.

Hint: It could be possible that the security settings of your SAP GUI for Windows doesn’t allow to create an object as in my case. If it is so and you want to experiment with this approach, disable this settings temporary for the time of your experiment. And don’t forget to enable it after your experiment.

<26=Automation(Error) : ERROR OCCURED IN MODULE: [Python Demo COM Server]
<26=Automation(Error) : PROGRAM_ID      |MODULE_NAME                |METHOD_NAME      |ERROR DESCRIPTION                  |VERSION                    |GUI VERSION       |MODULE_PATH              |
<26=Automation(Error) : Python.Demo     |Python Demo COM Server     |CREATEOBJECT     |Creation of control "" denied!     |Version info not found     |7500.1.3.1138     |Module doesnot exist     |
<26=Automation(Error) : "CreateObject"
                        #0: STRING "Python.Demo"
                        #1: LONG "1033"
                        could not create object


Addendum 2019/04/29

It tried this approach with Python 3.7.3 and Python for Windows 224 and it works only with x86. With Python 3.6.8, 3.7.2 and Python for Windows 224 all works well and as expected.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Michelle Crapo
      Michelle Crapo

      One word - Cool!   This looks great,


      Author's profile photo Javier Caceres
      Javier Caceres

      good tip

      Author's profile photo Heng Li
      Heng Li

      Cool, but i am using python3.7.1 it shows can't found

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Heng Li,

      this makes no difference, it works also with Python 3.7.x. Do you installed PyWin32 successfully?

      Best regards

      Author's profile photo Praveer Sen
      Praveer Sen

      Hello Stefan,

      I have created COM Server using given python code and registered in SAP application server OS and restarted system and server.

      But on execution the given ABAP program, it's giving SY-SUBRC = 3 error code.

      Is there anything I am missing in OS or SAP side.

      Best Regards,



      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Praveer,

      sy-subrc 3 means problems with memory allocation at the presentation server. I don't know what could be the reasons.

      Best regards

      Author's profile photo Praveer Sen
      Praveer Sen

      Hi Stefan,

      There was some mistake while registering COM server. Now I have successfully registered COM server.

      And it's working fine in VBScript code.

      But getting SY-SUBRC = 2 error code on abap side and OLD HANDLE  is giving -1 value.

      Do we need to maintain Python.demo entry in SOLE t-code, if yes, please share the configuration information.

      Best Regards,



      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Praveer,

      do you disable for your test the security settings as I described in my hint?

      Best regards

      Author's profile photo Shivam Shukla
      Shivam Shukla

      This one is awesome Stefan , I just saw , Did you ever get a chance to execute any ML Stuff using this way , if not then let's do it together 🙂 what do you say.


      Before anything Thanks a lot for sharing this Valuable stuff.



      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Shivam,

      thanks for your reply.

      No, I don't used this interface for ML. I am an ML novice. I tried a little bit with AI face detection, but not more. Sure, let us do it something together.

      Best regards

      Author's profile photo Robert Anglberger
      Robert Anglberger

      Hi Shivam and Stefan,

      last week i implemented my first ML application where I implemented Pyhton code in the Jupyter notebook to use the PAL functions from the HANA server by using the pyhton hana_ml library.


      I am very interested to automate this process so that I can execute the pyhton code e.g. from AMDP on the server without the need of client connection.

      Do you have any documents or examples for that or do you have some hints how to do that?




      Author's profile photo Shivam Shukla
      Shivam Shukla

      Hi Robert ,


      need to Analyze your query , will try to get back to you with something positive.




      Author's profile photo Takashi Matsumoto
      Takashi Matsumoto

      Hello Stefan,

      I'd like to do the same thing in a Linux environment.
      Please tell me what kind of solutions are available.

      Best regards,

      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Takashi,

      I assume that is not possible because Linux doesn't support COM natively.

      Best regards

      Author's profile photo Takashi Matsumoto
      Takashi Matsumoto

      Dear Stefan

      Thank you for your reply.

      Yes, that's exactly what you can't do with COM in a Linux environment.

      However, we are investigating whether there is a way to execute Python directly from within ABAP
      source in the same way as Windows in Linux environment.

      Please let me know if you have any good ideas.

      By the way, I confirmed that you can execute Python from an external command referring to your blog. Thank you.

      Best regards,


      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello Takashi,

      thanks for your reply.

      A tiny request: You want to execute Python on the front-end server (client) on Linux OS from ABAP in the back-end, correct?

      I assume you use SAP GUI for Java on your front-end server, correct?

      Thanks and best regards

      Author's profile photo Takashi Matsumoto
      Takashi Matsumoto

      Dear Stefan,


      Thank you for your quick reply.


      Yes, that's right.

      We are using S / 4HANA (1709). I want to run Python on the operating system Linux.

      And we use SAP GUI for Windows (750).


      Best regards,



      Author's profile photo ensar uslu
      ensar uslu

      We created a com server, and we have done all the steps you did (we use spyder as python compiler) but we can not get a result from abap.