Run python script

Hello everyone,

I am trying to execute a python script from a spring boot project. I tried the following and seems like it is not working even though I am not getting any error. Could some one guide on a way to do the task? here is what i tried.

package Python;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;

public class RunPythonScript implements JavaDelegate{
@Override
public void execute(DelegateExecution execution) throws Exception {
String ShellCommand = “cmd /c python PythonScript.py ExportedData.csv” ;
Process process = Runtime.getRuntime().exec(ShellCommand);

}

}

Note that code works well when executed as a simple java project while having the python script in the same directory. However, once I want to integrate the code as a process in a camunda schema it is not working.

The python is a simple code that creates a .txt file.

1 Like

I have tried another approach that is supposed to work. I replaced the system task with a script task and tried to use groovy code. I am getting an error which is: Can’t find scripting engine for ‘groovy’: scriptEngine is null. I tried to add the groovy dependencies but now I am getting: Dependency ‘org.codehaus.groovy:groovy-all:2.4.5’ not found.

Could anyone help? note that it is a springboot project

Here’s the task:

def pmdCommand = “python C:/Users/abdallah.daaboul/Documents/PredictionsPythonCode/PythonScript.py”
def sout = new StringBuffer()
def serr = new StringBuffer()

def process = pmdCommand.execute()
process.consumeProcessOutput(sout, serr)
process.waitForProcessOutput()
System.out << sout.toString()

and the dependencies:

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.4.5</version>
  <scope>test</scope>
</dependency>

Hi @Daaboul
Generally speaking the easiest and most maintainable way to run python with Camunda is to use an external task client. Then you don’t need to go near any dependencies or deal with trying to add python to the engine itself.

Check out this post for more details.