Skip to main content

Get Rolling With Python on IBM i

An introduction to Python on IBM i.

A graphic of a grey computer, with blue and yellow squiggles on the screen.

At the 2015 COMMON Annual Meeting, IBM announced it would be porting and supporting the Python programming language on IBM i. In late June, it delivered on that promise. One question some might be asking is “why?” Don’t we already have PHP as an alternative to RPG? And Ruby? And Node.js? Well, the question could be flipped around to ask why we as RPG coders don’t code in COBOL, or vice versa. One common reason for picking a language over another is the overall personality of the language and community. The same is true when it comes to open-source languages. Everyone has opinions. Each language has core focuses and strengths.

For example, wikipedia.org has this to say about Python:

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.

You could almost replace “Python” with “Ruby” in this description because they’re so similarly defined. This excites me because Ruby ranks high on my programmer-happiness scale! And nobody likes a crabby-pants programmer.

IBM’s developerWorks page for Python has instructions for obtaining and installing Python 3.4, the latest version available. You’ll notice it’s furthering the 5733OPS licensed program by adding it as Option 2. I’m impressed at how quickly IBM has been delivering open-source functionality—and I think it’s just getting started.

Once Python is installed, you can start kicking the tires by locating the Python runtime, namely the python3 binary. The which command is good for that:

$ which python3
/QOpenSys/usr/bin/python3

The python3 binary doesn’t actually reside at that location and instead is symbolically linked to /QOpenSys/QIBM/ProdData/OPS/Python3.4/bin/python3. We can see the symbolic link by using the ls -l command:

$ ls -l /QOpenSys/usr/bin/python3
lrwxrwxrwx    1 qsys     0                
98 Jun 18 16:50 /QOpenSys/usr/bin/python3 -> /QOpenSys/QIBM/ProdData/OPS/Python3.4/bin/python3

Or we could combine the two commands together with the pipe(|) and xargs capabilities:

$ which python3 | xargs ls -l
lrwxrwxrwx    1 qsys     0                
98 Jun 18 16:50 /QOpenSys/usr/bin/python3 -> /QOpenSys/QIBM/ProdData/OPS/Python3.4/bin/python3

You can learn further details about Python by specifying the --version option:

$ python3 --version
Python 3.4.2

Next, let’s put together a small Python program. Create file hello.py and occupy it with the following code:

---hello.py---
print('hello')

Go back to your shell and invoke hello.py as follows:

$ python3 hello.py
hello

It worked!

The print statement sent string ‘hello’ to the console as expected. The other useful way to test your Python code is to use the included REPL (Read Eval Print Loop) utility (a.k.a., interactive console). Type python3 without any parameters to enter the REPL utility. Now enter Python statements and have them be immediately processed, as shown:

$ python3
Python 3.4.2 (default, Jun 12 2015, 19:07:14) [C] on aix6
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')
hello
>>> quit()
$

The >>> characters convey you’re within the Python interactive console. To return to the PASE shell, type quit(). As you can see, it gives you immediate output. This is really nice not only for learning Python but also debugging and unit testing your code.

Creating With Pip

To round out this article, let’s create a small Web application. You can accomplish this task in many ways including using the Bottle Web framework, which IBM has included with the Python installation. I want to show how the pip command works so we’ll be using an alternative Web framework.

Pip is short for “Pip Installs Python” and is a package manager that makes it dead-simple to obtain functionality from other open-source projects onto your IBM i. It’s worth noting there isn’t a command literally named ‘pip’; instead ‘pip3’ correlates to the version of Python that exists in license program 5733OPS. If desired, you could create a pip symbolic link that points to pip3. This will create a symbolic link in /QOpenSys/usr/bin, which is already in your PATH environment variable and will thus resolve appropriately when run from the command line:

$ ln -s /QOpenSys/QIBM/ProdData/OPS/Python3.4/bin/pip3 /QOpenSys/usr/bin/pip

To test the newly created symbolic link and make use of pip, let’s install the lightweight Web framework Appier, as shown:

$ pip install appier
Downloading/unpacking appier
 Downloading appier-0.9.41.zip (134kB): 134kB downloaded
 Running setup.py (path:/tmp/pip_build_usrbskbi/appier/setup.py) egg_info for package appier

Installing collected packages: appier
 Running setup.py install for appier

Successfully installed appier
Cleaning up...

The pip install appier command goes out to the online repository for Python packages, checks to see if one exists named ‘appier’, and if so downloads and installs it into the default location. You can see the installation location using the pip show command:

$ pip show appier
---
Name: appier
Version: 0.9.41
Location: /QOpenSys/QIBM/ProdData/OPS/Python3.4/lib/python3.4/site-packages
Requires:

Now that Appier is installed, continue with the instructions from their readme.md file, which tells us to create a new file and paste this content into it:

---hello.py---
import appier

class HelloApp(appier.App):

   @appier.route("/", "GET")
   def hello(self):
       return "Hello World"

HelloApp().serve()

I had a situation on my machine where I needed a different HOST value specified when Appier started. By following their online instructions, I learned an appier.json file in the root directory of my project was necessary. You can change several configuration options. For the sake of example, I specified three of them:

---appier.json---
{
 "LEVEL" : "DEBUG",
 "HOST"  : "0.0.0.0",
 "PORT"  : 8080
}

Now it’s time to start up this small Web app by issuing the following command. You’ll notice the ‘3’ at the end of the python3 command. You could also create symbolic link /QOpenSys/usr/bin/python to keep from typing the ‘3’ like we did for the pip command.

$ python3 hello.py
2015-07-14 17:01:57,360 [INFO] Booting appier 0.9.41 (CPython 3.4.2.final aix6)...
2015-07-14 17:01:57,360 [INFO] Starting 'HelloApp' with 'legacy'…

Point your browser at http://your_ibmi_ip:8080 and you should see what’s shown in Figure 1.

iaug15EXTRA1article2FIG1.png
Figure 1

If you determine Appier isn’t for you, then simply uninstall it with the following command. As you can see, these package managers make it simple to share code.

$ pip uninstall appier
Uninstalling appier:
 /QOpenSys/QIBM/ProdData/OPS/Python3.4/lib/python3.4/site-packages/appier-0.9.41-py3.4.egg-info
 /QOpenSys/QIBM/ProdData/OPS/Python3.4/lib/python3.4/site-packages/appier/__init__

… omitting entries for sake of brevity
 /QOpenSys/QIBM/ProdData/OPS/Python3.4/lib/python3.4/site-packages/appier/validation.py
Proceed (y/n)? y
 Successfully uninstalled appier

Keep Learning

That concludes this introduction to Python on IBM i. It’s noteworthy to declare the Python port to IBM i includes capabilities to easily connect to DB2 for i and call upon RPG via what’s call the iToolkit. I’ll cover these in future articles.

Many resources are available for learning the foundations of Python syntax and language constructs. One I like is codeacademy.com, which is free and includes tutorials that can be done entirely through the browser. It’s also worth noting there’s a LinkedIn group named IBM i Python devoted to discussion. Also, the Litmis team has created an open-source repository where IBM i folks can share Python code.

Stay tuned for more and if you have questions, please shoot me an email at abartell@krengeltech.com.

        
IBM Systems Webinar Icon

View upcoming and on-demand (IBM Z, IBM i, AIX, Power Systems) webinars.
Register now →