Quantcast
Channel: blueleftistconstructor » Cutting Edge
Viewing all articles
Browse latest Browse all 5

*nix Jython – developing the RIGHT way

$
0
0

That title there, it’s a bit of a stretch you say? Well I’ve done enough development, especially in Java and Python, to know there’s more to it than running an installer. To really make use of a “development platform” many tools are needed. You’ve got IDEs like Eclipse, or editors like VIM or Emacs. Then there are libraries. You need a way to easily manage them, in Java I use Maven or Ivy, in Python I swear by VirtualEnv and VirtualEnv Wrapper.

I mean to use this post for reference in others, so it’s a bit of a first step to getting ready to use Jython as a god honest enterprise application platform. Eventually I’m going to post up tips on using Pylons and SQL Alchemy to build a simple app. I’ll even throw in some Java libraries so we can see why Jython’s worth while.

Let’s begin. The first step is to go to the Jython website and download the latest 2.5 version of Jython. I actually got the 2.5.2 RC 2, since it was the latest and works fine. By the time you are reading this you might be downloading 2.5.2 proper. Now you’ll want to run the jar from a terminal, not by clicking on it. Also, run it as root so that you can install it under /usr/local:

sudo java -jar <jython jar>

This’ll bring up an install, I install the whole shebang including source. IF your not the type to look through source code well then DO NOT install it. Once you’ve got Jython installed, somewhere like /usr/local/jython-xxx create a link from /usr/local/bin.

sudo ln -s /usr/local/jython-xxx/bin/jython /usr/local/bin/jython

You SHOULD have /usr/local/bin in your PATH, therefore now you’ll be able to open a new shell and type jython and voila! a Jython interpreter appears.

Now we need to make Jython a bit more useful. We don’t want to go installing every Python lib into the root site-packages. That’s a recipe for disaster if you plan on having multiple apps an/or libs on your system which could potentially use varied versions of a single library. No sweat VirtualEnv is the answer. Before we install it we’ll want to install some tools for managing packages. I had to use ez_setup.py to get easy_install (and setup tools) installed. You can download that here from PEAK once you’ve got the script run it:

sudo jython ez_setup.py

What you’ll end up with is a copy of “easy_install” in the bin folder of the Jython install. Check it right now, make sure it’s there. Now we are going to want to be able to invoke this from any old shell, so let’s again make a link, this time for easy_install:

sudo ln -s /usr/local/jython-xxx/bin/easy_install /usr/bin/jeasy_install

WOAH what’s up with the J? I name it jeasy_install for the following reason. I still want to be able to use C Python, and because of that I need to have easy_install work for it. If I was to name our link above ‘easy_install’ well then when I use that command libraries would be installed into the Jython ‘site-packages’ rather than C Python. So we need to be able to work with both, hence jeasy_install. When you use jeasy_install the Python packages are installed for use in Jython. That is what we want.

Now that we’ve got easy install we can add some other packages, first of which is going to be VirtualEnv, type the following:

sudo jeasy_install virtualenv

What that’ll do is install ‘virtualenv’ under your Jython install’s bin folder. Again we want to make a link to /usr/local/bin so that we can use virtualenv:

sudo ln -s /usr/local/jython-xxx/bin/virtualenv /usr/local/bin/jvirtualenv

And again, we affix a ‘j’ to the command so that it’s not overwriting our C Python virtualenv command. Now we are ready to rock! So far we’ve had to use ‘sudo’ so that we can install these Jython packages. With VirtualEnv that’s no longer an issue.

Change directory to a folder you don’t mind using for project stuff, we are going to make your first Jython virtual environment. This is a tool that allows us to install versions of Python libs for use in a single (or small number) of projects. If you’re interested you should take a look at the website. Input the following in your shell:

jvirtualenv messingaround

What that’ll do is create a folder named ‘messingaround’. It will contain the libraries you install while using the virtual environment. To use it type the following:

source /path/to/messingaround/bin/activate

You should notice your shell’s prompt change, it will now display the name of the virtual env you are using (messingaround). To stop using the virtualenv you can input deactivate. To make sure all is well try typing in “which jython” and you should see the output be a location in your virtual env. This means all is well. Now you are ready to install your first package in the virtual env.

Now this is a bit confusing so read carefully. When you create a virtual env, and then activate it, you will use the command easy_install now, rather than jeasy_install. You probably shouldn’t have to use jeasy_install at all now. This is because the virtual env creates it’s own alias of easy_install, and while the virtual env is activated that is what is used. Input “which easy_install” and you should see the path is in the virtual env. If not something is wrong.

So with that said. You’ll want to now install Pygments a neat syntax highlighting library for Python. To do so make sure you have the virtual env we created activated and then input:

easy_install pygments

You should see a bunch of output as it’s installed. Once it’s done you will want to open a Jython interpreter by inputing ‘jython’ to the shell. Then issue the following:

import pygments

This should simply return, if it fails then you will have to reread the directions and find what went wrong. On success you’re ready to now create a project that uses Jython in place of C Python.

You might want to deactivate and delete the messingaround folder we created. You might not need Pygments :) If you’ve followed this post and all works, you should be able to move onto some others tuts I’m writing about Jython that’ll build off of this one. Until then Happy Hacking!


Viewing all articles
Browse latest Browse all 5

Trending Articles