Introduction
Pyramid is a Python web framework from the Pylons Project that is designed to start small and grow with your application. It provides two routing mechanisms (URL dispatch and traversal), a flexible security system, and an extension architecture that allows developers to choose their own ORM, template engine, and session backend.
What Pyramid Does
- Routes requests via URL dispatch (regex patterns) or traversal (resource trees)
- Provides a pluggable authentication and authorization policy system
- Supports view configuration via decorators, imperative calls, or ZCML
- Offers asset specifications for locating templates, static files, and resources
- Includes a scaffolding system (cookiecutter templates) for project bootstrapping
Architecture Overview
Pyramid is a WSGI framework built around a Configurator that registers routes, views, and policies. At request time, the router matches a URL to a route or traverses a resource tree, selects the appropriate view callable, applies security predicates, and returns a Response. The framework uses a component architecture (based on Zope interfaces) for extensibility, allowing add-ons to hook into nearly any processing stage.
Self-Hosting & Configuration
- Requires Python 3.8 or later
- Install with
pip install pyramidor from a cookiecutter template - Configuration is done in Python via the Configurator object
- Deploy with any WSGI server: Gunicorn, uWSGI, or Waitress (included)
- Use
pserve development.iniduring development for auto-reload
Key Features
- Dual routing system: URL dispatch for REST APIs, traversal for content-heavy apps
- Security policies are pluggable and decoupled from views
- Extensible via add-ons that hook into the configurator without monkey-patching
- First-class support for internationalization and localization
- Comprehensive test harness with
pyramid.testingfor unit and functional tests
Comparison with Similar Tools
- Django — opinionated full-stack with built-in ORM; Pyramid is choose-your-own-components
- Flask — micro framework for simple apps; Pyramid scales better to complex architectures
- FastAPI — async-first API framework; Pyramid is WSGI-based with deeper extensibility
- Tornado — async server with its own event loop; Pyramid runs on standard WSGI servers
- web2py — batteries-included with admin UI; Pyramid favors explicit configuration
FAQ
Q: When should I choose Pyramid over Django? A: Choose Pyramid when you need flexibility in component selection (ORM, template engine) or when your routing needs go beyond simple URL patterns (traversal).
Q: Does Pyramid support async views? A: Pyramid 2.0 added initial async view support. For fully async applications, consider pairing with an ASGI adapter or using a dedicated async framework.
Q: What is traversal routing? A: Traversal walks a tree of resource objects using URL path segments as keys. It is useful for CMS-like applications where URL structure mirrors a content hierarchy.
Q: Is Pyramid still actively maintained? A: Yes. The Pylons Project maintains Pyramid with regular releases and security patches.