Quick and Dirty and Works!
So if you are reading this, and using Jython, you’ll want to use Java jars I’ll wager. Jython’s major selling point for me was being able to leverage a ton of libraries that I’ve gotten used to while working in the Java-sphere. This is pretty simple. When Jython boots up it looks for a variable named CLASSPATH, which you’ll configure in your Bash shell. What I’ve done in most projects up to now is create a folder named lib, although perhaps java_lib would be a better name, and drop all the jars I’ll need in there. Then I usually whip up a script like this:
CLASSPATH= for e in lib/*.jar; do CLASSPATH=$e:$CLASSPATH done echo "Adding jars: $CLASSPATH" export CLASSPATH=$CLASSPATH
Which when run from the project’s folder will properly setup the variable. Now you can run Jython and be able to import and run libraries from your jars.
Could We Do this Better?
Geez I sure hope so. One other need that’s not fully addressed by any Jython tool yet (that I know of) is jar dependency management. It’s easy to create Python projects that are aware of the libraries they’ll need to run; most of you have to have run into a setup.py file and installed libraries using it. Well if your project works by using a number of Python and Java libs, how do you suppose that you’ll install the Java libs? Of course we’ve already talked about just including them all in your project in a ‘lib’ folder. But this isn’t really the best option for anything but the simplest projects. For one, your SVN/HG/GIT will be bloated by the actual libraries being included. It is also quite easy to end up with various copies of the same jar on your machine, and the confusion of managing them all in such a hands on way.
One way we can deal with this issue today is by using the Maven tool. Right away I should say this’ll complicate the install process, since you’ll have both setup.py and pom.xml files describing Python and Java libs respectively. Then also note you’ll have to run two command to get both Python and Java libraries installed. As far as I know though it’s one of the only ways to go toolwise currently. I won’t go into the technical details of using Maven here, mostly because it’s probably not a good idea if you are a proper Python programmer to learn Maven. It’s convoluted at best, and really a beast of Java bureaucracy.
I think, if it’s possible maybe the community could whip up a little plug-in to easy_install or pip or both (does either of them allow plugins?) The convention used by the Java dep management tools to figure out how to obtain a jar if pretty basic. You have a repository, and in that you have a simple file system for getting a jar you want. Most Python people are only used to downloading their libs from Pypi (a lib repository), but in the Maven world it’s much more intense and you can have lot’s of repos (and by golly there are!) So what would work best, in my opinion, is to be able to alter a setup.py and include references to Java jars. First thing that is needed is a way to spec the repositories in setup.py, with a name/url pair. Then there is the dependencies themselves. With setup.py we’re used to just having a name, and a version. In Java it’s a group name, a artifact name (same as the setup.py name) and a version. We would need to way to add in this information, and also to set a 1 to 1 with a repository. So once you have the location of a repo (URL) you can get a jar by using the following path template
Also useful might be the information that the “artifact-version.jar” could be “artifact-version-sources.jar” for the jar containing all the sources for the library. This isn’t always available but if it is it’ll probably we worth grabbing.
So that’s about all there is to it. We could update the tools to be Jython sensitive and allow for spec’ing these that’d be a big step in building more professional Jython projects.