# A Little OffCode, Computers, Photography and Guns

16Apr/090

## Compiling Python (w/Jython)

If you're interested in the cross-platform-y-ness of python, you'll probably find this interesting. Initially i've written a few programs for work in python. They needed to be more or less cross-platform and simple to use since it's going to be distributed to users who have pretty much no idea how to do anything in the command line or anything of that nature.

My first experience with packaging python such that it could run on Windows desktops without installing the python interpreter before hand involved using a program called py2exe. Which does just what it says, it compiles all the dependencies and libraries of a particular python program into a single zip file along with a few basic things and generates an executable file for Windows based systems. The only drawback to this is that it only seems to work with Python 2.5 right now, it also has several extra files that must be distributed with the program and are really good at confusing users.

As for running python on Linux and OS X based systems it was a breeze, I had written a few very basic interfaces using Tkinter which is installed by default with python. The scripts by themselves run great on Linux and OS X systems since both come with them by default (RedHat Desktop is the Linux distro in question).

I still wanted an easier // more fool-proof way to distribute the scripts between computers with different flavors of OS's without having to have a different version for each OS. A co-worker suggested I look at Jython which to my surprise happens to be awesome. Jython is a full implementation of python on the Java VM. This is great for me because ALL of the systems I'm writing this stuff for have Java. You're probably wondering why I'm not just writing this stuff in Java to begin with and my answer to that is, these are really simple tasks that take way too much code//time to implement in Java when they took a few hours and about a 4th of the amount of code to perform the same task in python.

I discovered much to my surprise that it ended up being as simple as switching from Tkinter to Swing for the GUI's and then fixing a few random things like OS detection for configuring default paths of certain system files before the code all ran flawlessly in Jython. Now onto the really fun part. I discovered that I could compile the code including all dependencies and libraries for the Jython interpreter into a single Jar file usually about 1MB total. All the user has to do is double-click on the jar and everything fires up and does it's thing.

The first problem I ran into though was that OS X distributes their own version of the Java VM, 1.5.0_16 instead of the latest 1.6.0_13. I eventually discovered that it was as simple as adding the -target option to the java compiler and telling it to compile for the target version. Once all that was said and done I had a single jar file that contained all the necessary files to run the program by itself without any external dependencies.

If you're interested in knowing what command I use to compile to jar it is as follows:

 1 C:\Program Files\Jython221\jythonc.bat -J "-target 1.5" --jar "$(NAME_PART).jar" --all --core "$(FULL_CURRENT_PATH)"

Mind you that I run this in Notepad++ on the current open file. $(NAME_PART) is the name portion of the filename excluding the extension.$(FULL_CURRENT_PATH) is you guessed it: the full current path including the filename and extension.