Guru: Passing Parameters To Python
August 5, 2019 Mike Larsen
Python scripts offer great benefits to developers, whether used standalone or in conjunction with RPG programs. As I’ve been exploring Python and recently I had a need to execute a Python script from an RPG program. In addition to executing the script, I also wanted to pass it parameters.
One of the benefits of passing parameters is to give us the ability to soft-code programs or scripts. Soft-coding makes programs more flexible and re-usable, and helps to reduce maintenance.
To illustrate how to do this, I’ve coded a very simple RPG program and an even simpler Python script. I’ll start by showing the Python script, and then I’ll show how to execute it from RPG using QShell.
This story contains code, which you can download here.
I start by importing the “sys” Python modules, shown in the first piece of code, which allows me to print to the terminal and grab the parameters.
The rest of the code show next is just printing the parameters to the terminal. I told you it was a very simple script!
print (sys.argv) # prints the name of the Python script print (sys.argv) # prints parameter 1 print (sys.argv) # prints parameter 2 print (sys.argv) # prints parameter 3 print (sys.argv) # prints parameter 4
To show the results of the script, I can execute it from SSH Terminal in ACS (Figure 1).
I call my script parameters1.py and pass it four parameters (“lexie”, “slayer”, “yankees”, and “two words”). You may be wondering why I enclosed the fourth parameter — “two words” — in quotes. I did this because Python interprets a space as the end of a parameter. If I hadn’t enclosed the fourth parameter in quotes, Python would have seen these as two separate parameters and I would have actually passed five parameters instead of four.
Looking at the results, you’ll see that there is an additional piece of information that was displayed on the terminal: “parameters1.py”. That’s because the actual first argument passed to the Python script is the name of the script. It printed because I coded the script to print the argument zero, as shown in the first line of code from Figure 2.
Now that the Python script is working, it’s time to see how we can execute this from RPG. I populated four variables with the parameter values I’m sending to the script, seen in the next piece of code below. In a production process, these would likely be passed in as parameters or retrieved from a table, but I hard-coded them here to make the example easier to follow.
dcl-s firstParameter char(10) inz('lexie'); dcl-s secondParameter char(10) inz('slayer'); dcl-s thirdParameter char(10) inz('yankees'); dcl-s fourthParameter char(10) inz('two words');
Next, I define the path to my Python script as well as the name of the script.
pathToPython = 'python3'; pythonScript = 'parameters1.py';
The last part of the RPG program calls QShell to execute the Python script.
// Use Qshell to execute the Python script. // // Note: Parameters are detected by a space in Python, so the // 'fourthParameter' needs to be in quotes since it has two // words separated by a space. if I didn't put it in quotes, // Python would see them as two separate parameters. CmdStr = 'Qsh Cmd(' + quotes + %trim(pathToPython) + ' ' + %trim(pythonScript) + ' ' + %trim(firstParameter) + ' ' + %trim(secondParameter) + ' ' + %trim(thirdParameter) + ' ' + '"' + %trim(fourthParameter) + '"' + ''')'; Callp Run(Cmdstr:%Size(CmdStr));
When I call the RPG program from a command line, it opens a QShell terminal to show the results (Figure 2).
I received the same results as I did when I ran the Python script directly, and that is exactly what I expected.
So, I executed my RPG program from a command line on IBM i, but I thought it would be cool to try something else. What if I wanted to call my RPG from SSH Terminal? Let’s give it a shot.
It turns out that all I have to do is execute the system command to call the RPG program (Python_ex1) and the results of the Python script are printed to the terminal (Figure 3).
Pretty cool, right?
Although the example is pretty simple, you should now have the tools you need to build production processes that use both RPG programs and Python scripts together. The complete code for the Python script and RPG program used in this article is available for download.