Guru: Getting Started With Python On IBM i
June 10, 2019 Stephanie Rabbani
Python is one of my favorite open-source languages. I’ve used it to build web applications, utilities, and diverse tools such as a web server monitor and an automated application testing suite. In this article, I’ll show you how to install Python on IBM i and create your first program.
Why Python? For several reasons.
- You can use Python to do some cool processing on your system. Graphical application development, games, data analysis, and web development are all Python’s strengths.
- Coding in Python is easy, relatively speaking. The syntax is easy to learn, dropping the formal verbosity found in traditional coding languages.
- It’s a deeper well for new talent. This means it’s more welcoming to beginners, as indicated by Python’s consistent rank as one of the fastest growing languages.
- Python unlocks new ways to optimize and streamline workflow and operations, evolving IBM i so it keeps its throne over the next few decades. Open source is the foundation of the cloud computing world; it’s why IBM has embraced open source technologies in its Power roadmap. Modern languages will influence and evolve how your company interacts with its data.
The Basics Of Python
Python uses very simple syntax:
- It uses spacing instead of brackets to denote code blocks, such as if statements and loops.
- It’s case-sensitive.
There’s no need to explicitly declare variables. Variables are declared when you assign a value. Values are assigned to variables using a single equal sign (=).
habs = 24
Python is case-sensitive, so Habs is not the same as habs. Comments are preceded with a hash mark (#) and can follow on the same line of code.
habs = 24 #number of Stanley cups won by Montreal
Or . . .
habs = 24 #number of Stanley cups won by Montreal if habs < 25: print("they’ve been golfing since 1994") if habs > 24: print("I'll be old or dead before this is true, so you'll likely never see this message")
Your first Python program can include a built-in Python server.
run(host='0.0.0.0', port=8228, debug=True, reloader=True)
Setting Up Python On IBM i
Getting started with Python on IBM i is dead simple. Just follow two simple steps.
Step 1: Download and install YUM, an open source package manager.
IBM delivers open source software via YUM, so it’s where you’ll find Python 3 and other open source goodies.
Download YUM here: https://www-01.ibm.com/support/docview.wss?uid=nas8N1022619. Your system must be at IBM i 7.2 or later.
Important Note: The most secure, stable way is to run YUM and Python is in SSH. IBM i’s Business Architect of open source technologies, Jesse Gorzinski, explains the fundamentals of shells in this blog post, including how to choose and set your shell.
It’s also possible to install via QP2TERM().
Step 2: Install the Python package with YUM
- From Access Client Solutions, IBM’s free GUI for managing your IBM i:
- Go to the IBM i Tools menu Open Source Package Management.
- At this point, a list of currently installed Open Source packages will be displayed. If Python3 isn’t there, click the “Available Packages” tab.
- If you skipped the first step, clicking Open Source Package Management in the Tools menu will present a message asking if you’d like to install YUM.
- Go to the IBM i Tools menu Open Source Package Management.
- From the Command line it’s as simple as typing:
yum install python3
Now you can download python packages by pip3 install.
Python programs are typically .py extensions.
Optional Step
If you want to avoid having to specify the full path to Python every time you run it, add /QOpenSys/pkgs/bin to your path.
You can use this shell command to change the path:
PATH=/QOpenSys/pkgs/bin:$PATH export PATH
Your First Python program
- From the 5250 command line, run the Edit File (EDTF) command to open the IFS editor.
EDTF STMF('/home/steph/hello.py')
From the blank EDTF screen, press F15=Services to go to EDTF Options, where you should change the CCSID of hello.py to 1208 (Unicode), a Python-friendly encoding, as shown below:
- On the first line, type the following:
print("hello world")
Press F3 to save and exit.
- CALL QP2TERM or ssh to access PASE, and key the following command to run the Python program.
python3 /home/steph/hello.py
An IBM Champion and senior consultant for Seiden Group, Stephanie has developed web applications on the IBM i for over 15 years. Her expertise encompasses PHP, Python, DB2, SQL, RPG, Git, WebSmart, IBM i integration techniques and more. She develops a broad range of applications—shopping carts, ordering, warehousing, dashboards, mobile/responsive applications, Salesforce integration, and more—for small to Fortune 500 companies.
The steps 2 and 3 in “your first python program” appear to need lowercase (print instead of Print and python3 instead of Python3). And the EDTF command created the file in CCSID 37 which apparently python3 does not understand directly. In EDTF, I used Services (F15) to change the file CCSID to 1208 (UTF-8) and then I was able to run the program.
Thanks, Mike! The article has been updated as you suggested, including setting CCSID=1208.
thank you for the article. used this as my excuse to try the Python waters again. don’t know if it’s helpful to anyone else. had to use python3 instead of Python3 and changed the line of code from Print to print to get it to work.
Great read..thanks for the article..
Mark, you’re right. Those “P”s have been made lowercase now.
What an half baked article is this ?
yum install python3
Now you can download python packages by pip3 install.,.. what does this step says.. where to download python3 packages, and where does it go /