• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • 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.

    import sys 
    

    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[0]) # prints the name of the Python script
    print (sys.argv[1]) # prints parameter 1
    print (sys.argv[2]) # prints parameter 2
    print (sys.argv[3]) # prints parameter 3
    print (sys.argv[4]) # prints parameter 4
    

    To show the results of the script, I can execute it from SSH Terminal in ACS (Figure 1).

    Figure 1. Execute Python script

    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).

    Figure 2. Results from the Python script

    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).

    Figure 3. Calling an RPG program from SSH Terminal

    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.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, Python, qshell, RPG

    Sponsored by
    Raz-Lee Security

    Protect Your IBM i and/or AIX Servers with a Free Virus Scan

    Cyber threats are a reality for every platform, including IBM i and AIX servers. No system is immune, and the best defense is prompt detection and removal of viruses to prevent costly damage. Regulatory standards across industries mandate antivirus protection – ensure your systems are compliant and secure.

    Get My Free Virus Scan

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Four Hundred Monitor, August 5 Midrange Dynamics Speeds Table Updates In Db2 Mirror Clusters

    6 thoughts on “Guru: Passing Parameters To Python”

    • Justin Taylor says:
      August 5, 2019 at 10:24 am

      Is there a way to get the results into the RPG? I have a very basic way, using UNIXCMD (https://github.com/Justin-E-Taylor/RPG-calling-Python). Richard Schoen has a much more comprehensive option (https://github.com/richardschoen/PythonOniLibrary).

      Viva la Python! 😀

      Reply
      • Mike says:
        August 10, 2019 at 9:17 am

        I haven’t attempted this yet, but these look like good options!

        Reply
    • Roger Nassar says:
      August 6, 2019 at 8:03 am

      Thank you. How can python pass parameters to RPG/COBOL?

      Reply
      • Mike says:
        August 10, 2019 at 9:25 am

        Great question, Roger. I’ll do some research on this and write an article if I find a good solution.

        Reply
    • Kevin says:
      September 30, 2019 at 1:14 pm

      Great Article. FYI I had to change variable pathToPython from ‘python3’ to ‘/QOpenSys/pkgs/bin/python3’
      in the rpg program. For some reason OS cannot find python3 when the QSH command is executed.

      Reply
    • James Thomas says:
      March 2, 2023 at 4:36 pm

      I tried this, but got error qsh: 001-0019 Error found searching for command call. No such path or directory. My guess is that I have to establish a PATH

      Reply

    Leave a Reply Cancel reply

TFH Volume: 29 Issue: 44

This Issue Sponsored By

  • Maxava
  • WorksRight Software
  • RPG & DB2 Summit
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • Stacking Up IBM i And Windows In The Clouds
  • Midrange Dynamics Speeds Table Updates In Db2 Mirror Clusters
  • Guru: Passing Parameters To Python
  • Four Hundred Monitor, August 5
  • IBM i PTF Guide, Volume 21, Number 30

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle