Python Pyramid - Creating A Project

Last Updated : 3 Aug, 2026

Pyramid is a lightweight and flexible Python web framework used for building web applications and REST APIs. It is designed to scale from small projects to large enterprise applications while giving developers the freedom to choose the components they need. Pyramid follows a minimal approach, making it suitable for both beginners and experienced developers.

Installation

Before creating a Pyramid project, install the framework using pip.

pip install "pyramid[cookiecutter]"

To run Pyramid applications, you will also need the Waitress WSGI server.

pip install waitress

Note: It is recommended to create and activate a virtual environment before installing Pyramid to keep project dependencies isolated.

Why Use Pyramid?

Pyramid is designed to be simple for small applications while remaining powerful enough for large-scale projects.

  • Flexible: Lets developers choose the libraries and components they want to use.
  • Lightweight: Includes only the essential features needed to build web applications.
  • Scalable: Suitable for both small projects and enterprise-level applications.
  • Secure: Provides built-in security features such as authentication, authorization, and CSRF protection.
  • Extensible: Offers a rich ecosystem of third-party packages and extensions.

Creating a Pyramid Project

Pyramid provides a Cookiecutter template that generates a complete project structure. Run the following command:

cookiecutter gh:Pylons/pyramid-cookiecutter-starter

During project creation, Cookiecutter asks for details such as:

  • Project name
  • Package name
  • Template engine
  • Python version
  • Author information

After entering the required details, the project is generated automatically.

Project Structure

After creating a Pyramid project, the generated project contains several files and folders that help organize the application.

myproject/

├── myproject/
│ ├── __init__.py
│ ├── views.py
│ ├── routes.py
│ ├── templates/
│ └── static/

├── development.ini
├── production.ini
└── setup.py

Explanation:

  • __init__.py: Initializes the Pyramid application and configures routes, views, and settings.
  • views.py: Contains the view functions or classes that process incoming requests.
  • routes.py: Defines the URL routes used by the application.
  • templates/: Stores HTML template files used to render web pages.
  • static/: Contains static resources such as CSS, JavaScript, images, and fonts.
  • development.ini: Stores settings used while running the application in development mode.
  • production.ini: Stores configuration settings for deploying the application in a production environment.
  • setup.py: Contains the project's package information and dependency configuration.

Installing Project Dependencies

After the project is created, navigate to the project folder.

cd myproject

Install the project dependencies.

pip install -e .

This command installs all required packages and registers the project in editable mode.

Running the Development Server

Start the development server using:

pserve development.ini

Open the following URL in your web browser: http://localhost:6543/

Output

ss8
project output

Explanation:

  • pserve starts the Pyramid development server.
  • development.ini contains the application's development configuration.
  • By default, the application runs on http://localhost:6543.
  • Any code changes are reflected after restarting the server.

Understanding Default Project

When the project is created, Pyramid automatically generates a starter application containing a default route and view. The application typically includes:

  • A home page
  • Static file support
  • HTML templates
  • Application configuration
  • Development server settings

This provides a ready-to-run project that can be extended by adding new routes, views, templates, and database support.

Creating a View

Views process incoming requests and return responses.

Python
from pyramid.view import view_config
@view_config(route_name="home")
def home(request):
    return {"message": "Hello, Pyramid!"}

Explanation:

  • @view_config registers the function as a view.
  • route_name="home" connects the view to a URL route.
  • The returned dictionary is passed to the template for rendering.

URL Routing

Routes map URLs to specific views.

Python
config.add_route("home", "/")
config.add_route("about", "/about")

Explanation:

  • Creates the / route.
  • Creates the /about route.
  • Each route can be connected to a different view function.

Serving Static Files

Pyramid can serve static resources such as CSS, JavaScript, and images.

Python
config.add_static_view(
    name="static",
    path="static"
)

Static files are stored inside the static/ folder and can be accessed through the browser. Example:

http://localhost:6543/static/style.css

Applications

Pyramid is suitable for developing many types of web applications.

  • REST APIs: Build scalable API backends.
  • Business Applications: Develop ERP, CRM, and management systems.
  • Content Management Systems: Create CMS platforms with custom features.
  • E-commerce Websites: Build secure online stores.
  • Enterprise Applications: Develop large, modular web applications.
  • Microservices: Create lightweight backend services.
Comment