Welcome to the ultimate Python Pyramid web framework beginner guide. Whether you’re transitioning from Flask or Django, or diving into web development for the first time, Pyramid offers a flexible, modular, and “start‑small‑grow‑big” philosophy that adapts to projects of any size. In this article we’ll walk through everything you need to know to get up and running quickly: installation, core concepts, routing, views, templating, and best practices for building production‑ready Pyramid apps. By the end, you’ll have a solid foundation to start creating robust, scalable web applications with Python’s powerful Pyramid framework.
Why Choose Pyramid?
Pyramid stands out in the crowded Python web ecosystem for several reasons:
- Flexibility: Use only the components you need—no forced ORM, templating engine, or authentication system.
- Scalability: Start with a single‑file app and effortlessly scale to large, modular projects.
- Explicitness: Pyramid follows the “explicit is better than implicit” mantra, making code easier to read and maintain.
- Rich Ecosystem: A mature set of add‑ons (e.g.,
pyramid_jinja2,pyramid_tm,pyramid_authsanity) and strong community support.
Getting Started: Install Pyramid in Minutes
Before writing any code, you need a clean Python environment. The recommended way is to use venv (or virtualenv) to isolate dependencies.
Step‑by‑step installation
# Create and activate a virtual environment
python3 -m venv env
source env/bin/activate # Windows: env\Scripts\activate
# Upgrade pip and install Pyramid with a starter template
pip install --upgrade pip
pip install "pyramid==2.0" # Adjust version as needed
pip install waitress # Production‑grade WSGI server
# Verify the installation
pserve --version
With Pyramid installed, you can generate a minimal project using the official scaffolding tool:
# Install the scaffold generator
pip install cookiecutter
# Create a new project from the Pyramid starter template
cookiecutter gh:Pylons/pyramid-cookiecutter-starter
Answer the prompts (project name, package name, etc.), and Pyramid will create a ready‑to‑run directory structure.
Understanding the Core Concepts
Even though Pyramid is lightweight, it revolves around a few key concepts that you’ll encounter repeatedly.
1. Configurator
The Configurator object is the heart of a Pyramid application. It registers routes, views, and other components before the app is “frozen” into a WSGI callable.
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello, Pyramid!')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('home', '/')
config.add_view(hello_world, route_name='home')
app = config.make_wsgi_app()
# Serve with waitress
from waitress import serve
serve(app, host='0.0.0.0', port=6543)
2. Routes and Views
Routes map URLs to view callables. A view can be a simple function, a class, or even a method on a class. Pyramid’s routing syntax supports variables, predicates, and custom matchers.
- Static route:
config.add_route('about', '/about') - Dynamic route with placeholders:
config.add_route('profile', '/user/{username}') - Predicate example:
config.add_route('json', '/data', request_method='POST')
3. Request and Response Objects
Every view receives a Request object that gives you access to query parameters, headers, session data, and more. You return a Response object (or any object that can be rendered into one).
Adding Templating: Jinja2 Integration
While Pyramid ships with its own pyramid.renderers system, many developers prefer Jinja2 for its familiar syntax. Install the integration package and configure it as shown below.
# Install Jinja2 support
pip install pyramid_jinja2
# In your __init__.py (or wherever you configure the app)
from pyramid.config import Configurator
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('pyramid_jinja2')
config.add_jinja2_renderer('.html')
config.add_route('home', '/')
config.add_view('myapp.views.home', route_name='home', renderer='home.html')
return config.make_wsgi_app()
Now create templates/home.html:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Pyramid</title>
</head>
<body>
<h1>Hello, {{ request.authenticated_userid or 'Guest' }}!</h1>
<p>This is a Pyramid app using Jinja2.</p>
</body>
</html>
Managing Data: Choose Your Own ORM
Pyramid does not impose an ORM, giving you the freedom to pick the one that fits your project. The most common choices are:
- SQLAlchemy: Full‑featured, widely used, works well with
pyramid_tmfor transaction management. - SQLModel: A modern, Pydantic‑based layer built on top of SQLAlchemy.
- MongoEngine: If you prefer a NoSQL document store.
Below is a minimal SQLAlchemy example integrated with Pyramid:
# Install dependencies
pip install sqlalchemy pyramid_tm zope.sqlalchemy
# models.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key=True)
title = Column(String(200))
content = Column(String)
# __init__.py (configuration)
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('pyramid_tm')
config.include('pyramid_sqlalchemy')
config.add_route('list', '/articles')
config.add_view('myapp.views.list_articles', route_name='list', renderer='json')
return config.make_wsgi_app()
Handling Authentication and Authorization
Security is a core concern for any web app. Pyramid provides a flexible security policy that you can tailor to simple API keys or complex role‑based access control (RBAC).
Simple AuthTkt Example
# Install the auth package
pip install pyramid_authsanity
# In __init__.py
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('pyramid_authsanity')
config.set_authentication_policy('pyramid_authsanity.AuthTktAuthenticationPolicy',
secret='supersecret')
config.set_authorization_policy('pyramid_authsanity.ACLAuthorizationPolicy')
# Define a protected view
config.add_route('dashboard', '/dashboard')
config.add_view('myapp.views.dashboard',
route_name='dashboard',
permission='view')
return config.make_wsgi_app()
Define an ACL on a resource class to control who can access which view:
from pyramid.security import Allow, Authenticated
class RootFactory:
__acl__ = [
(Allow, Authenticated, 'view'),
]
def __init__(self, request):
pass
Testing Your Pyramid Application
Good test coverage ensures your app remains reliable as it grows. Pyramid works smoothly with pytest and provides a testing module for creating mock requests.
# Install testing tools
pip install pytest webtest
# tests/test_views.py
from pyramid import testing
def test_hello_world():
request = testing.DummyRequest()
from myapp.views import hello_world
response = hello_world(request)
assert response.status_code == 200
assert b'Hello, Pyramid!' in response.body
Run tests with pytest -q and watch the output. Integrating CI pipelines (GitHub Actions, GitLab CI) is straightforward and boosts confidence for production deployments.
Deploying to Production
For production you’ll typically run Pyramid behind a robust WSGI server like Waitress (pure Python) or Gunicorn (Unix‑focused). Containerization with Docker is also common.
Dockerfile Example
FROM python:3.12-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["waitress-serve", "--listen=*:8000", "myapp:main"]
Build and run:
docker build -t mypyramidapp .
docker run -p 8000:8000 mypyramidapp
Don’t forget to configure environment variables for secret keys, database URLs, and logging levels. Using a process manager like <
Leave a Reply