Python Web Framework Advice

Faced with an itch for learning, a looming project, or a cool startup idea, you want to create a website in Python. Good choice; Python will serve you well.

Scour the web you will, searching for cloud-parting, eye-opening wisdom that will lead you to the promised land, where the One True Framework will shine upon you and lead you to riches, fame, and world domination. Only one hurdle stands in your way:

No framework stands out as an unquestioned leader in Python web development as of 2009. To top that off, the top frameworks differ enough that you will miss out on cool features no matter which framework you pick.

Absorb that thoroughly. You’ll have to make a choice.

“There should be one — and preferably only one — obvious way to do it,” proclaims the Zen of Python. Understandably, web framework developers (myself included) often bristle during framework discussions. We await the day when a framework will capture the market and stand before us in obvious superiority.

Back to reality: Which Python web framework should you choose? Your project’s requirements will guide you along the way. The best framework for your project should become clear if you consult the requirements of your project.

The Frameworks

The Python.org Wiki includes a list of frameworks long enough to drive anyone into perpetual indecision. Disregard the unpopular frameworks; community support is essential. I suggest you decide between four frameworks: Django, Pylons, TurboGears 2, and web2py. I have experience with Django and Pylons, but I will also touch on TurboGears 2 and web2py for completeness.

Django

Django is the most popular framework, with extensive documentation and great community support. In its own words: “Because Django was developed in a fast-paced newsroom environment, it was designed to make common Web-development tasks fast and easy.”

Django includes an awesome admin interface, one of the main advantages it has over other frameworks. Django’s admin interface allows you to create/edit/update/delete your database entries online. This incredibly slick interface can be added to your project with a line or two of code. The Django admin wasn’t designed to handle all content entry for your site, though; it was designed with the common case in mind. If you need custom permissions, a different visual design, or other changes, you’ll have to either modify Django’s admin or address those areas yourself.

Django developed its ORM (and template language) from scratch. While that may have been a pragmatic decision at the time, Python now has SQLAlchemy, a superior database layer that has gained a lot of momentum. Django’s in-house ORM lacks multiple database support, and forces constraints on your database models (e.g. that every database table must have a single, integer primary key). If you choose Django, your project gains a near-inseparable dependency on Django’s ORM and database requirements.

If you accept those limitations, you gain access to Django’s many pluggable components. Django offers a comment system, user authentication and registration, RSS helpers, and more. These add-ons can speed up development time. But if you need to later customize those components, you may end up writing more code than if you had created those components yourself based upon your own requirements.

Pylons

Rather than encompassing everything into a monolithic safe haven, Pylons allows you to pick and choose the best Python components for your web project. Out of the box, Pylons supports Mako, a very fast templating engine, and SQLAlchemy, widely considered to be the best Python ORM available. SQLAlchemy includes multiple database support and just about any crazy combination of database requirements needed, and it handles ORM very well — yet it also allows you to provide raw SQL as needed.

Pylons has been criticized in the past for its loosely-coupled nature and incomplete documentation. But Pylons has picked up steam recently, introduced a new website and book and has improved its documentation tremendously.

I maintain that the Pylons is no more difficult to learn than Django. The ORM and templating language are independent projects apart from Pylons, whereas with Django they are bundled together. Django provides consistency, but Pylons provides flexibility.

Note well: Flexibility does not preclude consistency.

TurboGears 2.0

TurboGears 2.0, a ground-up rewrite of TG1, builds on Pylons to add features that a more centralized framework provides, such as form widgets and admin interface. Like Pylons, TG2 uses SQLAlchemy. If you like SQLAlchemy but aren’t comfortable with Pylons’ decoupled approach, consider TurboGears 2.

Ensure you look at the right version, though — TG2 is still in beta as of March 2009, and the TurboGears home page features TurboGears 1 most prominently. You’ll want to look at TurboGears 2.0 instead.

Make Your Choice

What do you need from your web framework?

If you’re building the next CMS or content-oriented site, focused on articles, blog entries, and the like, you’ll be more happy with Django than anything else. Django excels at content-management — that’s what it was designed for. Its admin interface can eliminate a lot of redundant content management work associated with a text-heavy site.

If you’re a startup aiming big, you would benefit from Pylons or TG2. You’ll want flexibility. With Pylons, you will have more control over your site — you won’t run into the framework’s limitations as much as you would using Django. Multiple database support may be essential if you need to scale. Pylons’ interchangeable nature means that you can exchange the ORM, templating language, or any other component as needed. If you really want flexibility, don’t let the framework get in the way. Pylons (or TG2) really shines in this area.

If you want something lightweight, either Pylons or web2py might fit the bill; Pylons doesn’t require you to use any database or templating language at all. When the rubber hits the road, though, you just want to get something done, and for a site with few requirements, any of the aforementioned frameworks would work well.

Different Philosophies

Django resembles Apple — always in control, presenting a cohesive, unified platform. But neither Django nor Apple strive to accommodate every use case; they strive to do a few things really well. Django handles content management better than any other framework.

Pylons adopts a Unix-like philosophy — loosely coupled components that work together to form a better whole. That allows Pylons to adopt the best components available, such as SQLAlchemy and Mako. Pylons won’t get in your way; that flexibility can be very valuable. Pylons’ improved documentation has reduced its learning curve substantially; learning Pylons should prove no more difficult than learning Django. Plus, if you learn Pylons, you’ll know how to use SQLAlchemy in Python projects that need database connectivity.

Look deeper than their respective “first blog” tutorials — any decent framework will let you create a blog in minutes. If you’re learning for fun, play with both. Good programmers use the best tool for the job.

Remember: Wading through framework blog entries won’t make your project progress any faster. Once you have evaluated your options, pick the framework that best suits your project. Then move forward.

In Python web development, there is more than one obvious way to do it.

You will not find the One True Framework. Not yet.

Published