September 19, 2018
Hot Topics:

Jython Reborn

  • May 23, 2008
  • By Chris McAvoy
  • Send Email »
  • More Articles »

Here's a fun game to play at your next developer cocktail party. If someone asks you what you're interested in, tell them "Jython." Inevitably, their response will be, "Jython is dead." Six months ago, I would have agreed, but with recent investment by Sun Microsystems, it appears that Jython is very much alive and ready to be used. I'll spend a little bit of time talking about some recent Jython history, and then compile the latest revision of Jython from source and play around with it a bit.

Jython is one of the many dynamic languages built on top of the Java Virtual Machine. The Jython project began in 1997. The original release was written by Jim Hugunin, who worked on it until 1999. Jim currently develops IronPython, the .NET implementation of Python.

Between 1999 and 2005, Jython passed among several lead developers, finally ending up with Frank Wierzbicki. In March of this year, Frank was hired by Sun Microsystems to work on Jython full time. Sun's recent hiring of two lead JRuby developers, plus the hiring of Ted Leung as "Principal Engineer, Dynamic Languages and Tools," shows just how serious Sun appears to be in its future support of dynamic languages on the JVM. Jython and JRuby aren't alone; the JVM language space is getting crowded, Scala, Rhino (a Javascript implementation written by the Mozilla Foundation), Clojure (a Lisp dialect), and Groovy all have their proponents.

The current release of Jython is 2.2, which conforms to the CPython 2.2 release. However, after 2.2, the Jython developers started to move in a new direction for the implementation, choosing ASM and ANTLR to speed development time. Although it's still in development, I'll focus on the 2.3 trunk version of Jython. A 2.5 implementation (again, that conforms to the 2.5 release of CPython) is slated to be the next official release). To get started on the latest version of Jython, you'll have to check it out of the Sourceforge repository. On Linux or OSX, you can execute the command:

svn co https://jython.svn.sourceforge.net/svnroot/

at the command prompt. On Windows, you can use TortoiseSVN to check out the repository. The next step is to compile the project into a jar file using Maven. Change directories into the maven directory in the project, then run mvn install. After the command is finished, you should have a directory in the root of the distribution called "dist," and a jar file "jython.jar" inside of it.

You can start the interactive interpreter by executing the jar file, like this:

cotton:dist cmcavoy$ java -jar jython.jar
Jython 2.3a0 on java1.5.0_13
Type "copyright", "credits" or "license" for more information.

Once in the interpreter, you can use it just as you would the CPython interpreter, with the extra feature of being able to import Java libraries, just as you would a Python library.

The interpreter is a great way to explore what Jython has to offer. However, Jython becomes very useful as a way to execute scripts. When writing a standard Python script, you can execute the script by running "python script.py", or by including "#!/usr/bin/env python" at the top of the script and making it executable. To execute a Python script with the Jython interpreter, you pass the script name as an argument to Java, like this:

java -Dpython.home=<path>/dist/ -jar <path>/dist/jython.jar

If you replace <path> with the base directory of your Jython distribution, you can execute Python scripts using Jython. The Jython wiki recommends a simple way to wrap the above in a shell script for easy execution. Create a file called jython that sits on your path; inside of it, put:


exec java -Dpython.home=<path>/dist/
   -jar <path>/dist/jython.jar $*

You have to replace <path> with the path to your Jython distribution, just as you did when you executed scripts by hand.

Now that you have the ability to execute scripts with Jython, see how it performs compared to CPython. You'll use a native Fibonacci calculator for an initial comparison. The script fib.py looks like this:

scratch = {}

def fib(n):
   global scratch
      return scratch[n]
   except KeyError:
      if n < 2:
         return n
      scratch[n] = fib(n-1) + fib(n-2)
      return scratch[n]

for i in range(50):
   print fib(i),

Notice that you keep a 'scratch' global dictionary as a poor man's memoizer, so that you don't recompute previously computed pieces of the sequence.

To carry out your tremendously unscientific speed comparison, you'll use the Unix 'time' utility. Here's the run in CPython:

cotton:play cmcavoy$ time python fib.py
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
   6765 10946 17711 28657 46368 75025 121393 196418 317811 514229
   832040 1346269 2178309 3524578 5702887 9227465 14930352
   24157817 39088169 63245986 102334155 165580141 267914296
   433494437 701408733 1134903170 1836311903 2971215073
   4807526976 7778742049

real  0m0.025s
user  0m0.013s
sys   0m0.012s

That's pretty fast. Now, the Jython run:

cotton:play cmcavoy$ time jython fib.py
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
   6765 10946 17711 28657 46368 75025 121393 196418 317811 514229
   832040 1346269 2178309 3524578 5702887 9227465 14930352
   24157817 39088169 63245986 102334155 165580141 267914296
   433494437 701408733 1134903170 1836311903 2971215073
   4807526976 7778742049

real  0m1.303s
user  0m1.065s
sys   0m0.099s

Not so fast. However, computing the Fibonacci sequence isn't really the strength of Jython. If you're looking for computational speed, you're much better off using lower-level languages. Jython's sweet spot is where development time needs to be fast, but infrastructure demands require big, well-supported containers to run everything in.


In many cases, running CPython isn't an option for a development team. Java has really become the lingua franca of the majority. Companies have invested heavily in Java infrastructures; writing software in these cases usually means writing to the JVM. Java is arguably not the world's most 'agile' language. Having options such as Jython and company to work with capitalizes on the big business buy in of Java with the speedy development time of Python.

About the Author

Chris McAvoy is a developer for PSC Group LLC in Chicago, Illinois. He specializes in Python and Ruby web applications. He also keeps a blog at http://weblog.lonelylion.com.

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date