Getting Started with Python on IBM i
Python has become an indispensable tool for modernizing applications on IBM i, thanks to its versatility, ease of learning, and extensive library support. IBM has recognized this and provides official support for Python through the Open Source Package Manager available in Access Client Solutions (ACS). This post will guide you through installing Python on your IBM i system, and demonstrate how to integrate Python scripts into your existing RPG and CLLE applications.
Installing Python via ACS’s Open Source Package Manager
IBM i’s Access Client Solutions (ACS) offers an Open Source Package Manager that simplifies the installation of Python and other open-source software directly on your IBM i system. Here’s how to get started:
- Open ACS and navigate to the Open Source Package Management tool.
- Select your system and connect to it.
- Look for the python39 package in the available package list. This package includes Python 3.9, which is the version recommended by IBM and comes with pip, Python’s package manager, making it easy to install additional Python libraries.
- Install the python39 package by selecting it and following the on-screen instructions to complete the installation process.
Once installed, you can access Python 3.9 from a PASE shell by typing python3
or python3.9
.
Integrating Python with Your IBM i Applications
Python can enhance your RPG and CLLE applications by adding new functionalities that are easier to implement in Python or by leveraging Python’s vast ecosystem of libraries.
Here’s a basic example of RPG code calling a Python script. This example assumes you have a Python script named hello.py
in the IFS.
This RPG snippet constructs a command string to call hello.py
using the system()
procedure, which executes the command in a PASE environment.
For more robust integration, including error handling and logging, consider encapsulating the call in a procedure like CallPython
and add it to a SRVPGM
. This example also includes writing the call details to a DB2 table named PYTHON_LOGS
.
PYTHON_LOGS Table Definition
First, create the PYTHON_LOGS table in your DB2 database to log details about Python script executions. This table will record the script path, parameters passed to the script, the result code of the execution, and a timestamp.
Here’s how you can enhance the RPGLE code to include parameter passing to the Python script and logging execution details to the PYTHON_LOGS table:
This approach not only executes the Python script but also allows for logging and error handling based on the result of the system()
call.
Calling Python from a CL program involves using the QSH
command to execute the script in the PASE environment. Here’s a CLLE example:
This CLLE program executes a Python script located at /path/to/hello.py
using the Qshell (QSH
) command.
Conclusion
Integrating Python into your IBM i environment opens up a world of possibilities for modernizing your applications and leveraging the extensive ecosystem of Python libraries. Whether you’re extending RPG applications or automating tasks with CLLE, Python can provide powerful functionalities to your IBM i system.