You are currently on IBM Systems Media’s archival website. Click here to view our new website.

IBM i > DEVELOPER > GENERAL

Get Rolling With Python on IBM i


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.

Aaron Bartell is Director of IBM i Innovation for Krengel Technology Inc. and an IBM Champion.



Like what you just read? To receive technical tips and articles directly in your inbox twice per month, sign up for the EXTRA e-newsletter here.



Advertisement

Advertisement

2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Are You Multilingual?

Rational enables development in multiplatform environments

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters