Write Python Script
How to write a python script for python4j

Introduction

Writing a python script in python4j involves first understanding what variables you want to pass in and what variables you want to retrieve, very similar to writing any function in a programming language. In order to learn more about this, please see our execution overview
When writing a python script, a user should try to write the script to be as minimal as possible. Focus on the minimal set of inputs, outputs, and code you want to run within a python script. As this is an embedded interpreter, too many complexities arise when trying to run a full blown application. Some complexities include garbage collection understanding and debugging script execution
If you are using external libraries, then you need to understand how our custom python path support works.

Before you write your first script

It is advised to test your python script in a real environment first. This would mean testing in a code editor and debugger for python like pycharm or visual studio code. This will also help you to determine what the script should look like for python4j. The following considerations should be thought about:
    1.
    inputs: the inputs to the script will be passed in from java and should not be declared as explicit variables in your script that's running embedded. These variable declarations will be dynamically created and inserted in to a real python script that gets executed by our execution framework.
    2.
    outputs: the outputs of the script will be passed from real python memory and are kept in memory within the scope of the try/with execution block.
    3.
    dependencies: the dependencies of your application should be bundled separately. The developers recommend a standalone miniconda installation for the target operating system. This version should match the version of cpython provided by python4j to avoid clashing. See [../reference/python-path] for more information on this topic.

Writing and executing your first script

Hello world is pretty straightforward. We'll do this write in line:
1
PythonExecutioner.exec("print('hello world')");
Copied!
This will do as you would expect and print hello world to the console.
Next , we can add concateneate 2 strings. In this example, we pass hello world in as strings. Note that we pass in 2 variables of type string:
1
try(PythonGIL pythonGIL = PythonGIL.lock()) {
2
List<PythonVariable> inputs = new ArrayList<>();
3
inputs.add(new PythonVariable<>("x", PythonTypes.STR, "Hello "));
4
inputs.add(new PythonVariable<>("y", PythonTypes.STR, "World"));
5
String code = "print(x + y)";
6
PythonExecutioner.exec(code, inputs, null);
7
}
Copied!
We could also pass in 2 ints and add them as well:
1
try(PythonGIL pythonGIL = PythonGIL.lock()) {
2
List<PythonVariable> inputs = new ArrayList<>();
3
inputs.add(new PythonVariable<>("x", PythonTypes.INT, 1));
4
inputs.add(new PythonVariable<>("y", PythonTypes.INT, 2));
5
String code = "print(x + y)";
6
PythonExecutioner.exec(code, inputs, null);
7
}
Copied!
The supported python types can be found here More on types can be found here
If we want to write an actual python script and have python4j load it, we need to read the script in to memory.
This can be achieved with the following:
1
String code = FileUtils.readFileToString(new File("path/to/pythonfile.py"), StandardCharsets.UTF_8);
Copied!
From there, we can pass the code to PythonExecutioner.exec(..) as follows:
1
try(PythonGIL pythonGIL = PythonGIL.lock()) {
2
List<PythonVariable> inputs = new ArrayList<>();
3
inputs.add(new PythonVariable<>("x", PythonTypes.INT, 1));
4
inputs.add(new PythonVariable<>("y", PythonTypes.INT, 2));
5
String code = FileUtils.readFileToString(new File("path/to/pythonfile.py"), StandardCharsets.UTF_8);
6
PythonExecutioner.exec(code, inputs, null);
7
}
Copied!

Retrieving results

Up till now, we haven't actually retrieved results from the python script, just passed them in. Below is how to retrieve results:
1
try(PythonGIL pythonGIL = PythonGIL.lock()) {
2
String code = "a = 5\nb = '10'\nc = 20.0";
3
List<PythonVariable> vars = PythonExecutioner.execAndReturnAllVariables(code);
4
}
Copied!
This will retrieve all results output from the executed python code. If you only want certain variables, then you can do the following:
1
try(PythonGIL pythonGIL = PythonGIL.lock()) {
2
List<PythonVariable> inputs = new ArrayList<>();
3
inputs.add(new PythonVariable<>("x", PythonTypes.STR, "Hello "));
4
inputs.add(new PythonVariable<>("y", PythonTypes.STR, "World"));
5
PythonVariable out = new PythonVariable<>("z", PythonTypes.STR);
6
String code = "z = x + y";
7
PythonExecutioner.exec(code, inputs, Collections.singletonList(out));
8
}
Copied!
Afterwards, you can read the value from out post execution using:
1
String value = out.getValue();
Copied!
Note that out is a parameterized type. When retrieving the value, the java runtime will automatically try to cast whatever the output result is from python to the specified type. For more information on types, please see our types reference
Last modified 2mo ago