Python/Web Applications

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

My Recommendation

If you are looking to build a web application in Python, my current recommendation is to use Pylons with Mako and SQLAlchemy.

However, I am shying away from Pylons. It's no longer so general. I am looking at something more generic such as web.py without the templates and DB stuff, or even CherryPy. But honestly, it's not rocket science to build something from BaseHTTPServer. It's nice to get the exception handling and quick-edit capabilities of Pylons, but I am sure that is a module somewhere you can import.

Setting up a new project

First, setup a clean environment using virtualenv. Note that we are setting up a clean environment, as if we just compiled python from source.

$ virtualenv --no-site-packages ~/Projects/your-project-here
$ cd ~/Projects/your-project-here
$ git init
$ echo ".pyc" >> .gitignore
$ git add .gitgnore *
$ git commit -m "Initial commit."

You may want to poke around in that directory to see what happened.

Start Work

To start working on your project:

$ cd ~/Projects/your-project-here
$ . bin/activate

This will setup the environment so that everything you do is run with your modules and not the modules from /usr/lib/python2.5/site-packages.

Stop Work

To stop working on your project:

(your-project-here)$ deactivate

Adding dependencies

After you've started work, just do this:

$ easy_install SQLAlchemy
$ git add lib/python2.5/site-packages/SQLAlchemy*
$ git commit -m "Added SQLAlchemy"

Separate Code from the Environment and Specific Condigurations

You'll want to develop the code somewhere other than the environment. Note that you can't run the code outside of the environment, though.

Use the environment git repository to keep track of dependencies you upgrade. You're tracking changes to the environment. If you make a mistake, you can revert to an earlier version.

Use the code to track changes to the code and make releases. The code is going to be structured like setup.py demands.

Configurations of particulare machines of classes of machines have their own python package that depends on your code and has its own special configuration files that are built programmatically upon install. Keep track of the software that manages this separately.

Deploying to a machine

When you're ready to duplicate your environment to another machine, you'll setup a virtualenv and then easy_install ONLY your configuration package. That should bring in all the dependencies.


Using RPM?

The problem with the RPMs that come with Fedora is that they tend not to be relocatable. Ideally, you'd want to put everything, and I mean EVERYTHING, into an RPM, and have those RPMs be relocatable. The issue here is the scripts and such within the virtualenvironment, which itself is an RPM. Not to mention the pesky easy-install.pth file that has to change with every package that is added.

What is Out There

  • Django -- I haven't tried it, and I probably will never try it because it seems like it is limiting. It may be right for you, though.
  • Gluon -- This seems to be more along the lines of Zope. Frankly, too much complexity and it looks like it is built by someone who doesn't have a lot of experience with real web apps.
  • Zope -- This is way too much complexity, and I didn't like it.
  • Plone -- This is Zope squared. I like it even less than Zope.
  • Myghty -- This used to be a pretty good way of building a web app, closely mirroring HTML::Mason. This is what I would've built a web app in 2 years ago.