Integrate Python into CL and RPG on IBM i

python trainingPython has been gaining momentum for building utility applications on IBM i, such as creating/reading Excel files, data transfer, process automation, calling REST APIs such as Salesforce and ServiceNow, and application monitoring.

Although some have said that Python would become the “new CL,” one limitation remains. While Python can easily call CL, RPG, and COBOL programs, calls in the other direction—from CL, RPG or COBOL to Python—required extra effort.

In this post we will introduce you to the PYRUN command, from Richard Schoen’s open source PythonOniLibrary (, which makes it easy for traditional CL and RPG programs to call Python utilities and use their output.

Before trying PYRUN, you must install the Python language itself, as described by Stephanie Rabbani in the linked IT Jungle Guru tip.

The challenge

Because Python runs in PASE, traditional IBM i jobs must spawn a new PASE job to run the selected Python script. The calling application can’t consume the PASE output without a little help.

A solution: PYRUN

The PYRUN command is a front-end wrapper command that does the following:

  • Allows various calling options, including the Python path and script name, a list of up to 40 input parameters as needed, and parameter switches that determine how PYRUN should return the Python script’s results.
  • Calls the Python script.
  • Captures Python’s output from standard output (STDOUT) and sends it to the requested location so that the output can be consumed by the calling job or other applications.

Choose where you would like your output

We’ve been calling PASE scripts (PHP, Python, Node, more) from CL and RPG for years, but PYRUN is special. With PYRUN, we can choose several ways to receive output from the script. By default, output is logged to the table QTEMP/PYSTDOUT. (On IBM i, every job has its own, unique QTEMP library.) PYRUN can also return output via several other options:

  • DSPSTDOUT – Display the outfile contents within your interactive (5250 mode) program (good for debugging.)
  • LOGSTDOUT – Place STDOUT log entries into the calling job’s job log.
    • Note: Don’t log to the job log if you expect your Python script to output thousands of lines to STDOUT. You will litter your IBM i job log with superfluous info in many cases or you may even kill the IBM i job if the job message queue hits its limit and the IBM i job is not set to wrap its job log. You can easily identify PYRUN’s messages by its CPF message ID of PYM9898.
  • PRTSTDOUT – Print STDOUT to a spool file. Handy for batch jobs.
  • DLTSTDOUT – This option ensures that PYRUN cleans up its internally used IFS temp files after processing.

Once your Python script has completed and the PYRUN command returns without any errors, your CL, RPG, or COBOL application can then read QTEMP/PYSDTDOUT to process any useful data such as return values.

Extend PYRUN using your own CL programs

You could make this even easier by writing your own wrapper program or CL command to call PYRUN to run a Python script, and then read PYSTDOUT for return values and send them back through your custom CL command return parameters. Example: say you wrote a Python script to read a database and generate an Excel file with an auto-generated name. You need to get the name back. Python could write the name to STDOUT so that the calling CL or RPG application could read it in and then do something with the new Excel file. There are limitless uses for running Python scripts from traditional IBM i applications and then consuming the result for use.

Create your own utilities with Python on IBM i

This post was just a quick introduction to the PYRUN command. In future posts, or during training or consulting engagements, we can teach you how to use PYRUN in your own applications. Enjoy Python on IBM i and send us your feedback.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.