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
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)
Parameters:
{FirstName: payload.EmployeeDetails.FirstName,LastName:payload.EmployeeDetails.LastName}
Below is how the configuration will looks like
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
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>
<scripting:parameters>#[{
firstName:payload.EmployeeDetails.FirstName,
lastName:payload.EmployeeDetails.LastName
}]</scripting:parameters>
</scripting:execute>
Error type : SCRIPTING:UNKNOWN_ENGINE
FlowStack : at pythonscriptpocFlow(pythonscriptpocFlow/processors/1 @ pythonscriptingpoc:pythonscriptpoc.xml:30 (Execute)).
Kindly suggest.
Hi Shani,
Please see the link – https://help.mulesoft.com/s/question/0D52T00004mXWnP/issue-in-scripting-module-of-mule-4
Check if you have created the correct config? otherwise refer the sample application in the POST.
Thanks,
Mulesy Team
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 »
Thanks for Sharing!
how to add python files here