Python Power Tools

An Introduction to Some Tools Available for Python Developers Running OS X

by Christopher Roach

Introduction

One thing that every craftsman loves are new tools, and programmers are no exception to this rule. If you fall into the category of a Python developer, then this article was written precisely for you. What I propose to do during the course of this article is to introduce you, the Python programmer, to a few libraries that will aid you in your development ritual and perhaps even inspire you to develop in new and interesting areas.

One thing you'll no doubt notice once we start our exploration into the tools available for Python developers, is that there is no limit to the number of libraries, packages, modules, etc. that you can use to get your job done. What I've tried to do here in this article is concentrate on the most popular and most stable, and then perhaps point out a couple of alternatives for those of you not content with using just the mainstream tools.

So, with our goal clear in our minds, let's press on, and begin our journey with a look at some of the GUI libraries available for Python on OS X.

GUI Libraries

GUI libraries are an area that in no way, contradict the observation I made in the introduction. There is a plethora of GUI libraries available to Python programmers, and many of these are open source and cross-platform so they can easily be used on the Macintosh OS, as well as many of the other operating systems that you may be forced to use outside of your own little world. A few of these libraries are Tkinter (the standard Python interface to the Tk GUI toolkit), wxPython (a Python wrapper for wxWidgets, a popular cross-platform GUI library written in C++), PyQt (a set of Python bindings for the Qt toolkit that is also cross-platform), and many more. I'll concentrate on two of the most popular libraries in the next two subsections of this article starting with Tkinter, and then I'll mention a few alternatives quickly in the final section.

Tkinter

As I stated in the previous paragraph, Tkinter is the standard interface to the Tk GUI toolkit for Python programmers. It is also pretty much the de facto standard for GUI development with Python on any platform. There are many advantages to using Tkinter as your GUI library of choice, when developing Python-based applications.

First, it's one of the most portable GUI libraries. Tcl and Tk have been around for such a long time, and have developed such a devoted following, that it is nearly impossible to name a platform that doesn't have a port of the toolkit available. Second, it's really easy to install on the Macintosh operating system; simplyinstall MacPython on your system, rather than going with the default version of Python. Finally, it's extremely easy to develop GUI-based applications with it. This is, of course, one of the reasons why Tk has such a large following amongst programmers. So, with all of these great benefits in mind, how do we get Tkinter installed?

Well, if you installed the MacPython binary--and I recommend that you do <http://homepages.cwi.nl/~jack/macpython/download.html>--you're already halfway there. Basically, you have Python and the Tkinter interface already, now all you need to do is get a copy of the Tk toolkit. To do this you'll need to download and install the latest distribution of the Tcl/Tk Aqua binary from <http://tcltkaqua.sourceforge.net> I installed the Batteries Included binary (~30MB), but you can probably get away with just installing the 5MB version, but really, why not install the whole thing and give yourself some more toys to play with later? You can always learn Tcl/Tk after toying around with your new Python libraries; after all you can never know too many programming languages.

Once you've got a version of Tk installed on your computer, the only thing left to do is enable the Tkinter binary module using the MacPython PackageManager application. To do this, find the MacPython folder on your system and double click on the PackageManager application. Once opened, you should see a package named _tkinter-x.x-binary in the package list. Select this package and click on the install button in the form below the package list. That's it. You should now be able to create Python programs with a Tk-based graphical interface. If you wish to try it out, you can just run the simple application below to see a quick "hello world" dialog.

Listing 1: TkinterSample.py

TkinterSample.py

Create a new Tk application with a root and label object and display it to the user.
from Tkinter import *
# All Tk applications should have a root
root = Tk()
# Create a new label, assign it to the root, and give it the text "Hello World"
w = Label(root, text="Hello world!", pady=10, padx=10)
# The command packs the root frame tightly around the label
w.pack()
# This command starts the main Tk event loop
root.mainloop()

To run this program, you'll need to run the script through the Python interpreter by typing pythonw TkinterSample.py into the Terminal. One thing to take notice of, is that we use something called pythonw rather than calling the normal Python interpreter to run our program. The reason for this is because our application is a window-based application (i.e., it does not display inside of the Terminal). The pythonw script executes the Python interpreter using a fully qualified path to overcome a bug within OS X. This allows a Python GUI-based application to interact properly with the Window Manager.

If you got everything typed in correctly, and you used pythonw instead of python to run your script, you should see a dialog that looks similar to the one in the figure below.

Figure 1 - TkinterSample.py and Emacs

Well then, you've tried out the short application above to make sure that your installation of Tkinter works properly, and you're still curious to learn more. Well have no fear, wxPython is another very popular GUI library for Python and we're going to cover it in the very next subsection.

wxPython

Back in 1992, at the Artificial Intelligence Applications Institute, at the University of Edinburgh, Julian Smart was designing a tool that needed to run on both Windows PCs and X-based Unix workstations. The existing cross-platform GUI libraries were all too expensive for an in-house experimental project, and so the decision was made to develop an easy to use, cross-platform, GUI library. Thus, wxWidgets was born--actually wxWindows at the time, the name was later changed.

Over time, the library developed a strong following, and in 1996, a version for the Python programming language was created by Robin Dunn. The port for Python was called wxPython, and it was implemented as an extension module wrapping the wxWidgets C++ class library. Since then, the wxPython library has grown to become a very stable, powerful, and easy to use GUI toolkit. Just like Tkinter, it has been ported to nearly every computing platform imaginable, which means that - when using wxPython - you'll be able to develop your GUI-based apps with little regard for the final target architecture or operating system.

The wxPython library is an Open Source project, so the source code can be downloaded and manipulated if need be, but the most important thing to remember is that Open Source means free, as in beer. Just like Tkinter, you can download, use, and freely distribute applications that you create with this library without paying a fee to anyone. The library is also extremely easy to install, since a binary installer is available for the Mac OS X platform which can be found at the main wxPython website <http://www.wxpython.org/>.

Through my experiments with wxPython, I have found that I prefer it to all of the other GUI libraries that I have tried thus far. I found its installation to be the easiest out of the GUI libraries listed in this article and its popularity is second only to Tkinter and rapidly gaining on the Tk toolkit. Also, just like Tkinter, there are ports of the library to several other popular languages. So, by learning Tkinter or wxPython, you are essentially getting a tool that can be used with several different languages and not just for Python development.

Other Libraries

As I stated in the section introduction, I decided to cover the two GUI libraries that I found to be the most popular. The reason for this was that, I had assumed (however wrong my assumption may be) that the most popular libraries would be the easiest to use, the easiest to find help for, the most stable, and the most available to the consumer of our applications. That said, there are several other very nice options for Python GUI-based development, a few of which we'll quickly look over in the remainder of this section.

For anyone who wants to develop applications specifically for the Mac OS X platform, PyObjC provides a bridge between the Python programming language, and the Cocoa Objective-C classes. With PyObjC installed, you can create OS X native applications using the Interface Builder application to create your GUIs. For those of you interested in using the PyObjC Bridge to create your Python applications, we'll be covering it in the next section on code editors and IDEs.

If you're not really interested in Cocoa development, you're looking for something a little more cross-platform, and you have experience with Java, you may want to look into Jython. Jython is an implementation of the Python language written in 100% pure Java, so it's actually much more than just a conduit to Java's Swing library, but for the purpose of fitting into this section, we'll look at as such.

Ok, I know what you're thinking. Why would I ever need a Java implementation of Python? Well, just let me point out a few of the reasons why you may find Jython useful when developing Python applications.

First, having Python implemented in Java means that you now have the ability to run Python applications on any system that can run the JVM. This opens up the opportunity of writing programs for many more platforms, since nearly every platform now runs Java.

Second--and what I really wanted to cover in this section--if you're like me and you basically cut your teeth on Java, then Jython gives you a good starting point to get up and running quickly with Python, without the additional overhead of learning a GUI library as well. When I first started learning Python, I was able to write GUI-based applications in Python using Swing--with which I already had quite extensive experience. This meant that I was able to concentrate more on learning the language, and less on learning a GUI library.

Finally, there's PyQt. Trolltech's Qt is a very popular GUI library for Linux programming, but it also happens to be a very able toolkit on several different platforms (including Windows and Mac OS X, of course). PyQt is a set of bindings for Trolltech's Qt GUI toolkit and like many of our other libraries, it's available on a non-commercial license for free, and can be downloaded at <http://www.riverbankcomputing.co.uk/pyqt/download.php> My only complaint about using Qt is that it doesn't seem to scale that well. Larger projects written in PyQt seemed a bit sluggish, even on a beefy G4, 1.25GHz processor and 1GB of RAM. Although, with that said, I have noticed that larger projects using some of the other libraries are not the speediest either. It would seem that very large-scale, GUI-based projects might still be out of reach of the Python developer for the time being; much like the Java-based apps of a few years ago (recently Java applications have become much more responsive, though still a little too slow for my taste).

Scientific Libraries

Since overhauling their operating system by adding Unix underpinnings to its already venerable user interface, Apple has been gaining ground in the scientific community. Researchers in Bioinformatics and Computational Biology, Applied Physics, and Mathematics--you name it--have found OS X to be a formidable system for research as well as their day-to-day tasks. The Mac OS provides a scientist with an all-in-one solution. OS X makes it easy for a researcher to run all of the Unix-based scientific apps they need, develop programs in a multitude of scripting and programming languages, write their research publications using Microsoft Word or LaTeX, and do it all on one machine. It is this versatility that has made OS X, the preferred operating system of many researchers.

One thing that you'll find common across most scientists is a need for powerful scripting languages. Bioinformatics, for example, is one area of research where we have seen a strong use of popular and powerful scripting languages. Perl has historically been the scripting language de rigueur for many bioinformaticions, but Python has been steadily gaining ground for several years now. Loved by many for its combination of power and readability, (the latter of which is something that many will say Perl definitely lacks) Python has grown on the scientific community, and we are starting to see several different libraries created specifically for the tasks required by these researchers.

The rest of this section will try to introduce you to a few of the more popular libraries for researchers. So, for any of you out there considering graduate school a possibility in the near future, listen up and take notes.

NumPy

Numerical Python, or NumPy, is a library created principally by Jim Hugunin while a student at MIT, and currently maintained by a group of developers headed by Paul Dubois. This library provides Python with the facilities to handle matrices and Linear Algebra mathematics. It's a powerful library that is extremely easy to install, as well as use.

Installation of the NumPy library is typical of most python command line installations, that is, you'll need to run a setup script through the Python interpreter with the command line argument install. In our case we will need to modify this a bit by using the setup_all.py script instead, and thus, our install line should look like this: python setup_all.py install.

Running this line from the NumPy directory in the Terminal application should install NumPy on your system without any problems. Before you get started with the installation, however, you may need to download the library. You can do so at its homepage at <http://numpy.scipy.org//>.

One more thing: you may need to be root to install the library. So, if you run into any problems during the installation, especially ones that mention invalid permissions, you may want to try running the script again, this time with the sudo command.

Once you get NumPy installed, and you're able to play around with it a bit, you'll notice how greatly it simplifies doing complicated linear algebra in your Python programs. However, even though it's great by itself, the true power of NumPy can be appreciated only when coupled with other libraries such as the popular DISLIN library--a library for data visualization. With that thought in mind, it only seems natural to look into the DISLIN library next.

DISLIN

DISLIN, as was mentioned in previous subsection, is a library for producing data visualizations. It's cross platform and also quite easy to use. Once again though, we have found a tool that makes use of X windows to produce its visual displays. So, remember when running your DISLIN visualizations, you must run them from whatever X windows implementation you have decided to install on your system.

Even with the caveat that we have to run our visualizations under a distribution of X windows, the library is definitely worth the download, if you plan on doing anything where it will become important to graphically visualize large sets of data. So, I would suggest that you run out to its homepage <http://www.mps.mpg.de/dislin/> and download the Darwin, ppc distribution for the Mac.

Once you've downloaded the library and unpacked it into a temporary directory, you can proceed with the installation of the package. Once again, this library will need to be installed from the command line (no binaries available, but hey its free, so stop complaining). This one is a bit more complex than the one for NumPy, but all of the steps are clearly labeled in the README file provided with the distribution. Just make sure that you don't stop after the installation, but that you also run through the directions for using DISLIN with Python that are listed below the install instructions.

Once you've got the library properly installed, you'll definitely want to give it a try. So, to satisfy your curiosity, and also as an example of the ease at which you can create impressive data visualizations using Python and DISLIN, I have included the code for a simple surface map visualization below.

The results of running the code above, through the Python interpreter from X11, can be seen in the figure below. Take notice of how very little code you need, to perform a complex visualization like the example that was provided. (See figure 2.)

Figure 2 - DISLIN surface_map.py Sample

Biopython

With the overwhelming popularity of the Bioinformatics field recently, I feel it is important to have a portion of our discussion look into at least one library for researchers in this field. Yet, keep in mind that I am not a researcher in the computational biology field (my chosen area of study is Computer Science), nor do I profess to understand all that I am about to cover below. Regardless of my ineptitude, however, I hope that at least a few bioinformaticions out there will find this subsection to be helpful and informative.

So, with that disclaimer out of the way (and hopefully all the hate mail from the bio-crowd avoided), let's take a look at the Biopython set of tools, and what they have to offer the scientific community.

First, Biopython refers to a project that brings together many developers of freely available Python tools for computational biology. Biopython also refers to the tool suite that is available online at the Biopython website <http://www.biopython.org>. There are several tools for running common operations on sequences as well as the data structures to represent them. There are tools for running translations and BLASTing and for performing classifications of data using k Nearest Neighbors, Naive Bayes, or Support Vector Machines. Biopython is an extremely large and comprehensive set of tools for the biological researcher.

For grins and giggles, I decided to download Biopython and try it out (I had recently begun looking into pursuing a bioinformatics path for my Ph.D., although I may be rethinking that again very soon). I found that the installation was actually not very difficult as long as you don't mind command line installations--sorry, once again no binary distributions here--however, it was very time consuming. Biopython has so many tools that it has quite a large set of dependencies, so I found myself downloading and installing quite a few other packages just to get it to work on my machine.

Nevertheless, after about an hour or so, I finally had it installed (with a few optional libraries left out) and I was able to write up a quick script that took a DNA sequence and returned its RNA translation. Not that I understood what I did, mind you, but I did feel like a scientist for a very short time, and in the process I found that the library should be very intuitive for anyone already possessing knowledge of the bioinformatics field.

Also, as a hint for anyone wishing to install this library, first download and install Fink (a project dedicated to bringing Unix Open Source software to OS X). I was able to use it to install most of the dependencies of Biopython, making the installation a heck of a lot simpler.

A Few Others

In the final section I wanted to just quickly mention a few other libraries that I found for Python, which were no less important than those in the sections prior, but since I'm not writing a book, I had to draw the line somewhere. So, this section just quickly introduces a few more libraries that I think are must haves for any serious Python programmer.

We'll start with a couple of libraries that allow Python scripts to access and utilize arguably the two most popular open source databases: MySQL and PostgreSQL. Then, we'll quickly look into two more libraries that allow the Python programmer to create 3D graphics on OS X.

Databases

In order to standardize the many modules that allow Python developers to access a database, a database API specification, which is currently in version 2.0, was developed. This makes Python programs that access a database, not only easier to write, but also infinitely more portable, since all that needs to change for the code to work with another database is the module that implements the specification.

In this section I wanted to quickly point out two modules that allowed Python programmers to access two of the most popular Open Source databases. These modules are: MySQLdb and PyGreSQL, both, of course, are compliant with version 2.0 of the Python Database specification.

The first, MySQLdb, is, of course, an interface to the MySQL database. This module is fully thread safe and supports transactions. It's easy to install and easy to use, and it can be used with any version of Python above v1.5.2, and with versions of MySQL v3.22 or greater. The other module I wanted to point out works with the PostgreSQL database. PyGreSQL is the name of the module that provides Python with the ability to access and utilize a PostgreSQL database.

If neither database is currently installed on your machine, and you're not particularly interested in going through a long install with the source, Marc Liyanage has links and instructions on his website <http://www.entropy.ch/software/macosx/> for downloading and installing each database on OS X with a binary installer.

There are several choices for graphics libraries when working with Python on the Mac. As I already mentioned earlier, Python developers on the Mac have access to the DISLIN visualization library. However, these obviously are not the only ones, in this section I'll quickly introduce two other 3D graphics libraries.

To start with, VPython is a data visualization library similar to the DISLIN library. However, its main aim is ease of use, and at least from my readings, it seems as if it is being geared towards students in the sciences. As for the installation of the library, my recommendation would be to install it from Fink (use the command fink install visual-py23), and that seems to be the general consensus since even the homepage of VPython <http://www.vpython.org> recommends the same.

The other library gives us access to what is probably the most popular cross platform graphics library: OpenGL. PyOpenGL binds Python to the OpenGL 3D graphics library, version 1.1. The library is a bit behind, since at last check, OpenGL was up to version 2.0. Nevertheless, it works well, it's cross platform, and it's extremely easy to use.

Once again, this library does not have a binary installer, so you'll have to download the source <http://pyopengl.sourceforge.net/> and build it. Nevertheless, when I built the library on my machine, the build and install steps went by without any incident whatsoever, and before I knew it, I had a sample Python-based OpenGL program up and running.

Conclusion

Well, we've certainly covered a lot of ground over the course of this article. I hope you've found some interesting new tools to play with in your future Python development. I also hope that I've inspired you to go out and start doing some research on the web to find even more new tools for your Mac. And who knows, perhaps you'll find a void somewhere out there in the tools available, and you'll be able to organize an effort that delivers another powerful library back to the Python community.

This article's main objective was to whet your appetite, and hopefully get a few of you to try something new in your daily development ritual. In the future, I'll be publishing a few articles that take a look at some of the technologies we covered here, a little closer. So, if you enjoyed this article, and you find yourself thirsty to learn some more, have no fear, I'll have a few more in depth tutorials out there for you to sink your teeth into, sometime very soon.

Christopher Roach recently earned his MS in Computer Science from Virginia Tech and currently works as a software engineer in Florida's Space Coast. On the weekend he tries to find time to write articles on Macintosh programming and do battle with insanely powerful hurricanes, while still trying to preserve some semblance of a life. If you have questions or comments on the article, you can email him at croach@vt.edu

Community Search:

MacTech Search:

Software Updates via MacUpdate

Kodi 15.1.rc1 - Powerful media center to...

Kodi (was XBMC) is an award-winning free and open-source (GPL) software media player and entertainment hub that can be installed on Linux, OS X, Windows, iOS, and Android, featuring a 10-foot user... Read more

Bookends 12.5.8 - Reference management a...

Bookends is a full-featured bibliography/reference and information-management system for students and professionals.
Access the power of Bookends directly from Mellel, Nisus Writer Pro, or MS Word (... Read more

Chromium 44.0.2403.125 - Fast and stable...

Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web.
Version 44.0.2403.125:
This release contains a number... Read more

iMazing 1.2.2 - Complete iOS device mana...

iMazing (was DiskAid) is the ultimate iOS device manager with capabilities far beyond what iTunes offers. With iMazing and your iOS device (iPhone, iPad, or iPod), you can:
Copy music to and from... Read more

Audio Hijack 3.2.0 - Record and enhance...

Audio Hijack (was Audio Hijack Pro) drastically changes the way you use audio on your computer, giving you the freedom to listen to audio when you want and how you want. Record and enhance any audio... Read more

FontExplorer X Pro 5.0.1 - Font manageme...

FontExplorer X Pro is optimized for professional use; it's the solution that gives you the power you need to manage all your fonts.
Now you can more easily manage, activate and organize your... Read more

Calcbot 1.0.2 - Intelligent calculator a...

Calcbot is an intelligent calculator and unit converter for the rest of us. Featuring an easy-to-read history tape, expression view, intuitive conversion, and much more!
Features
History Tape -... Read more

MTR 5.0.0.1 - The Mac's oldest and...

MTR (was MacTheRipper)--the Mac's oldest and smartest DVD-backup app--is now updated to version 5.001
MTR -- the complete toolbox, not a one-trick, point-and-click extractor. MTR is intended for... Read more

LibreOffice 4.4.5.2 - Free, open-source...

LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more

Adobe Lightroom 6.1.1 - Import, develop,...

Adobe Lightroom is available as part of Adobe Creative Cloud for as little as $9.99/month bundled with Photoshop CC as part of the photography package. Lightroom 6 is also available for purchase as a... Read more

Bandai Namco has released Pac-Man Championship Edition DX on iOS and Android, which features the classic arcade gameplay that we've all grown to love.
Pac-Man Championship Edition DX can be enjoyed in much shorter bursts than the arcade versions... | Read more »

Angel Stone is Fincon's follow up to the massively successful Hello Hero and is out now on iOS and Android.
You play as a member of The Resistance, a group of mighty human warriors who have risen up in defiance of the Demon horde threatening to... | Read more »

The not exactly rumors were true and the birds are back. Angry Birds 2 has come to the App Store and the world will... well I suppose it'll still be the same, but now we have more bird-flinging options!
[Read more]
| Read more »

You Could Design Your Own Card for Chain...

If you've ever wanted to create your own item, weapon, trap, or even monster for Chainsaw Warrior: Lords of the Night, this is your chance. Auroch Digital is currently holding a contest so that fans can fight to the death (not really) to see which... | Read more »

Bitcoin Billionaire is Going Back in Tim...

If you thought you managed to buy everything there is to buy in Bitcoin Billionaire and make all the money, well you though wrong. Those of you who made it far enough might remember investing in time travel - and it looks like that investment is... | Read more »

Domino Drop (Games)

Domino Drop 1.0
Device: iOS Universal
Category: Games
Price: $1.99, Version: 1.0 (iTunes)
Description:
Domino Drop is a delightful new puzzle game with dominos and gravity!Learn how to play it in a minute, master it day by day.Your... | Read more »

Best Buy has iPad Air 2s on sale for up to $100 off MSRP on their online store for a limited time. Choose free shipping or free local store pickup (if available). Sale prices available for online... Read more

B&H Photo has the 13″ 1.6GHz/128GB MacBook Air on sale for $899.99 including free shipping plus NY tax only. Their price is $100 off MSRP, and it’s the lowest price available for this model.... Read more

Worldwide Tablet Market Decline Continues, Ap...

The worldwide tablet market declined -7.0% year-over-year in the second quarter of 2015 (2Q15) with shipments totaling 44.7 million units according to preliminary data from the International Data... Read more

The Apple Store has Apple Certified Refurbished iPad Air 2s available for up to $140 off the price of new models. Apple’s one-year warranty is included with each model, and shipping is free:
- 128GB... Read more

Updated Apple iPad Price Trackers

We’ve updated our iPad Air Price Tracker and our iPad mini Price Tracker with the latest information on prices and availability from Apple and other resellers.
Read more

Apple refurbished 2014 13-inch 128GB MacBook...

The Apple Store has Apple Certified Refurbished 2014 13″ MacBook Airs available starting at $759. An Apple one-year warranty is included with each MacBook, and shipping is free:
- 13″ 1.4GHz/128GB... Read more

Apple’s Education discount saves up to $300 o...

Purchase a new Mac or iPad at The Apple Store for Education and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free,... Read more

MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders. Not responsible for typographical errors.

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.