How to write a python script for python4j
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.
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:
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.
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.
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.
Hello world is pretty straightforward. We'll do this write in line:
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:
We could also pass in 2 ints and add them as well:
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:
From there, we can pass the code to PythonExecutioner.exec(..) as follows:
Up till now, we haven't actually retrieved results from the python script, just passed them in. Below is how to retrieve results:
This will retrieve all results output from the executed python code. If you only want certain variables, then you can do the following:
Afterwards, you can read the value from out post execution using:
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