Execute Python Script In Mule 4


In this tutorial we will demonstrate how we can execute python script in mule 4 using scripting module

To use the script component we must need to add scripting module The Scripting Module executes custom logic written in a scripting language, Please visit how to add module in anypoint studio


Drag and drop http listener component from mule palette and configure this to trigger the request

Execute Python Script In Mule 4


Create the transform message component to pass the payload on which we will execute the script

%dw 2.0
output application/json
    "EmployeeDetails": {
        "EmpId": "1234",
        "FirstName": "Sachin",
        "LastName": "Tendulkar"


Drag and drop script component from mule palette

Write the python code under Code as shown below, here we are going to add the length of FirstName and LastName retrieved from payload and pass the total length to result variable
Python Code:

result=len(FirstName) + len(LastName)

Engine Name:  Jython(Python)

{FirstName: payload.EmployeeDetails.FirstName,LastName:payload.EmployeeDetails.LastName}

Below is how the  configuration will looks likeExecute Python Script In Mule 4


Set the Transform message component to set the result:

%dw 2.0
output application/json
    "Total Length":payload


Deploy the project and test from soapui


Sample Project: pythonscript sample application

Soapui project:invoke-python-script-soapui-project

Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.

Share this:
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Shani Jaiswal
Shani Jaiswal
1 year ago

Hi team,

With the latest version of Mule , I am getting this error :
ERROR 2020-09-15 19:03:56,039 [[MuleRuntime].uber.04: [pythonscriptingpoc].pythonscriptpocFlow.CPU_INTENSIVE @25d22d7b] [processor: ; event: 1a55ba30-f758-11ea-aa4d-f04da2a03ee2] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
Message        : Scripting engine ‘jython’ not found. Available engines are: Oracle Nashorn
Element        : pythonscriptpocFlow/processors/1 @ pythonscriptingpoc:pythonscriptpoc.xml:30 (Execute)
Element DSL      : <scripting:execute engine=”jython” doc:name=”Execute” doc:id=”8de9c9fd-1c3f-4788-b51f-f9427e079399″>
<scripting:code>result = len(firstName) + len(lastName)</scripting:code>
FlowStack       : at pythonscriptpocFlow(pythonscriptpocFlow/processors/1 @ pythonscriptingpoc:pythonscriptpoc.xml:30 (Execute)).

Kindly suggest.

1 year ago

There is one more way of doing a python script in mule. It can be done using a maven plugin and can run scripts during generate-resources phase. The script can generate api.properties files with values in it and also send email via smtplib module. This is especially useful in CICD pipelines. <plugin>       <groupId>org.codehaus.mojo</groupId>       <artifactId>exec-maven-plugin</artifactId>       <version>1.6.0</version>       <executions>         <execution>           <configuration>             <executable>python</executable>             <workingDirectory>src/main/resources/</workingDirectory>             <arguments>               <argument>my.py</argument>             </arguments>           </configuration>           <id>python_build</id>           <phase>generate-resources</phase>           <goals>             <goal>exec</goal>           </goals>         </execution>       </executions>     </plugin> my.py code below # coding=UTF-8 import os,io import smtplib cur_path = os.getcwd() #should be the src/main/resources with io.open(cur_path+”/api.properties”, mode=’w’) as the_file:   the_file.write(u’hello=maven’) from email.mime.multipart import MIMEMultipart from email.mime.text… Read more »