When you look into non-RPG languages for developing software on IBM i, you’re bound to find information about Java or PHP. It would appear that PHP was made popular on IBM i due to Zend’s efforts to port their PHP server technology to IBM i. IBM has also made languages such as NodeJs, Ruby, and Python available too. As a regular user of Python, I am very excited by this!
I initially tried to get Python working on PUB400, and while python3 was available, I was unable to get locate the necessary Python libraries/files/eggs to be able to run queries against files on my account. The required python library, ibm_db_dbi, is not available, or at least I could not find it after searching for some time. However, I found that Zend has some free courses to learn open-source on IBM i, and along with it you get an IBM i instance to log into to complete the course work.
Since I don’t have access to an IBM i system where I can go over installing the PTFs, I will write this assuming that the IBM i system already has the PTFs with open source and python installed.
To start using Python, SSH into the UNIX environment on the IBM i and get ready to add a repository to yum:
yum-config-manager –-add-repo http://public.dhe.ibm.com/software/ibmi/products/pase/rpms/repo
Next, install Python v3!
yum install python3
yum install python3-pip
This ensures that the python3 packages are installed, and the Python PIP tool (which will be needed for any serious Python development later). While you’re at it, go ahead and install the IBM packages needed to talk with the IBM i system database.
yum install python3-ibm_db
yum install python3-itoolkit
The first package, ibm_db, allows one to make connections to the database on the localhost system and run SQL queries on the files tables. The second package, which I’m not going to cover, allows for communicating with the system to run commands.
To start accessing your database tables, the below program is a very simple example. Keep in mind if you want to connect to the IBM i remotely, you need IBM’s Connect licensed product–and you’ll need to modify the connect() method arguments. If you just want to connect to the IBM i on the localhost, the below code works.
import ibm_db_dbi as db2
# Establish a connection to the DB2 system on localhost
db2_connection = db2.connect()
# create a cursor for working with the database
my_cursor = db2_connection.cursor()
# execute an SQL query
my_cursor.execute(“…SQL QUERY HERE…”)
# iterate over the results
for row in my_cursor:
#do something with output row
It’s a simple as that–just like if you were connecting to a PostgreSQL or MySQL server from Python on an x86_64 server! When I get more time I’d like to experiment with building some REST APIs, and I’d like to experiment with NodeJs on the system too.