I’ve been getting into Python this month, and loving it.  The language is, in a word, tight- textually compact, efficient to write, easy to read – it’s also fast, powerful, runs anywhere…

And you needn’t take my word for it. Python can lay claim to the mightiest of sponsors: Google, which uses Python as its primary scripting language, and which has hired Python’s creator, Guido van Rossum, as a full-time employee to further develop the language in-house at MountainView.

I’ve been using the website documentation and Dive Into Python online (excellent) to get started. I had trouble, however, finding some clear cut instructions on getting Python working at the server level- that is, setting up a Linux web server so that modules could be distributed across the system, as well as called with HTTP. This jump starter covers that and a few other tips I thought worthwhile.

Distributed Python Modules in Linux

A) Create packages

The first step is to create ‘package’ type directories for our Python modules.  We do this by putting a file named __init__.py in each directory we want to be a ‘package’ (The file can be empty or contain code- it is the first module loaded when a directory is imported)

B) Import Modules

Once our package directories are created, we can import the modules within them using dot notation:

import {package}.{module}

NOTE: package = directory name. module = file name without .py extension.

C) Register top level packages

Finally, in order to access the packages from outside the current directory, you’ll need to register them at the system level.

You’ve got several options here:

Option 1: Create a symbolic link in the site-packages file

$ sudo ln -s /{'package' directory} /usr/lib/python2.5/site-packages

Option 2: Modify the PYTHONPATH environment variable

(Haven’t tried this yet- will update when I do)

Option 3: Add module paths at runtime

import sys

sys.path.append(‘/{location of module}’)

More to come…

as always, expertise disclaimed, corrections encouraged  -Y