> Read the latest version of this page
Edit me on GitHub

Creating a Pyramid Project

As we saw in Creating Your First Pyramid Application, it’s possible to create a Pyramid application completely manually. However, it’s usually more convenient to use a scaffold to generate a basic Pyramid project.

A project is a directory that contains at least one Python package. You’ll use a scaffold to create a project, and you’ll create your application logic within a package that lives inside the project. Even if your application is extremely simple, it is useful to place code that drives the application within a package, because: 1) a package is more easily extended with new code and 2) an application that lives inside a package can also be distributed more easily than one which does not live within a package.

Pyramid comes with a variety of scaffolds that you can use to generate a project. Each scaffold makes different configuration assumptions about what type of application you’re trying to construct.

These scaffolds are rendered using the PasteDeploy paster script.

Scaffolds Included with Pyramid

The convenience scaffolds included with Pyramid differ from each other on a number of axes:

  • the persistence mechanism they offer (no persistence mechanism, ZODB, or SQLAlchemy).
  • the mechanism they use to map URLs to code (traversal or URL dispatch).
  • whether or not the pyramid_beaker library is relied upon as the sessioning implementation (as opposed to no sessioning or default sessioning).

The included scaffolds are these:

pyramid_starter
URL mapping via traversal and no persistence mechanism.
pyramid_zodb
URL mapping via traversal and persistence via ZODB.
pyramid_routesalchemy
URL mapping via URL dispatch and persistence via SQLAlchemy
pyramid_alchemy
URL mapping via traversal and persistence via SQLAlchemy

Note

At this time, each of these scaffolds uses the Chameleon templating system, which is incompatible with Jython. To use scaffolds to build applications which will run on Jython, you can try the pyramid_jinja2_starter scaffold which ships as part of the pyramid_jinja2 package. You can also just use any above scaffold and replace the Chameleon template it includes with a Mako analogue.

Rather than use any of the above scaffolds, Pylons 1 users may feel more comfortable installing the Akhet development environment, which provides a scaffold named akhet. This scaffold configures a Pyramid application in a “Pylons-esque” way, including the use of a view handler to map URLs to code (a handler is much like a Pylons “controller”).

Creating the Project

In Installing Pyramid, you created a virtual Python environment via the virtualenv command. To start a Pyramid project, use the paster facility installed within the virtualenv. In Installing Pyramid we called the virtualenv directory env; the following command assumes that our current working directory is that directory.

We’ll choose the pyramid_starter scaffold for this purpose.

$ bin/paster create -t pyramid_starter

The above command uses the paster command to create a project using the pyramid_starter scaffold. The paster create command creates project from a scaffold. To use a different scaffold, such as pyramid_routesalchemy, you’d just change the last argument. For example:

$ bin/paster create -t pyramid_routesalchemy

paster create will ask you a single question: the name of the project. You should use a string without spaces and with only letters in it. Here’s sample output from a run of paster create for a project we name MyProject:

$ bin/paster create -t pyramid_starter
Selected and implied templates:
  pyramid#pyramid_starter  pyramid starter project

Enter project name: MyProject
Variables:
  egg:      MyProject
  package:  myproject
  project:  MyProject
Creating template pyramid
Creating directory ./MyProject
# ... more output ...
Running /Users/chrism/projects/pyramid/bin/python setup.py egg_info

Note

You can skip the interrogative question about a project name during paster create by adding the project name to the command line, e.g. paster create -t pyramid_starter MyProject.

Note

You may encounter an error when using paster create if a dependent Python package is not installed. This will result in a traceback ending in pkg_resources.DistributionNotFound: <package name>. Simply run bin/easy_install, with the missing package name from the error message to work around this issue.

As a result of invoking the paster create command, a project is created in a directory named MyProject. That directory is a project directory. The setup.py file in that directory can be used to distribute your application, or install your application for deployment or development.

A PasteDeploy .ini file named development.ini will be created in the project directory. You will use this .ini file to configure a server, to run your application, and to debug your application. It sports configuration that enables an interactive debugger and settings optimized for development.

Another PasteDeploy .ini file named production.ini will also be created in the project directory. It sports configuration that disables any interactive debugger (to prevent inappropriate access and disclosure), and turns off a number of debugging settings. You can use this file to put your application into production, and you can modify it to do things like send email when an exception occurs.

The MyProject project directory contains an additional subdirectory named myproject (note the case difference) representing a Python package which holds very simple Pyramid sample code. This is where you’ll edit your application’s Python code and templates.

Installing your Newly Created Project for Development

To install a newly created project for development, you should cd to the newly created project directory and use the Python interpreter from the virtualenv you created during Installing Pyramid to invoke the command python setup.py develop

The file named setup.py will be in the root of the paster-generated project directory. The python you’re invoking should be the one that lives in the bin directory of your virtual Python environment. Your terminal’s current working directory must the the newly created project directory. For example:

$ ../bin/python setup.py develop

Elided output from a run of this command is shown below:

$ ../bin/python setup.py develop
...
Finished processing dependencies for MyProject==0.0

This will install a distribution representing your project into the interpreter’s library set so it can be found by import statements and by PasteDeploy commands such as paster serve, paster pshell, paster proutes and paster pviews.

Running The Tests For Your Application

To run unit tests for your application, you should invoke them using the Python interpreter from the virtualenv you created during Installing Pyramid (the python command that lives in the bin directory of your virtualenv):

$ ../bin/python setup.py test -q

Here’s sample output from a test run:

$ python setup.py test -q
running test
running egg_info
writing requirements to MyProject.egg-info/requires.txt
writing MyProject.egg-info/PKG-INFO
writing top-level names to MyProject.egg-info/top_level.txt
writing dependency_links to MyProject.egg-info/dependency_links.txt
writing entry points to MyProject.egg-info/entry_points.txt
reading manifest file 'MyProject.egg-info/SOURCES.txt'
writing manifest file 'MyProject.egg-info/SOURCES.txt'
running build_ext
..
----------------------------------------------------------------------
Ran 1 test in 0.108s

OK

Note

The -q option is passed to the setup.py test command to limit the output to a stream of dots. If you don’t pass -q, you’ll see more verbose test result output (which normally isn’t very useful).

The tests themselves are found in the tests.py module in your paster create -generated project. Within a project generated by the pyramid_starter scaffold, a single sample test exists.

Running The Project Application

Once a project is installed for development, you can run the application it represents using the paster serve command against the generated configuration file. In our case, this file is named development.ini:

$ ../bin/paster serve development.ini

Here’s sample output from a run of paster serve:

$ ../bin/paster serve development.ini
Starting server in PID 16601.
serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

By default, Pyramid applications generated from a scaffold will listen on TCP port 6543. You can shut down a server started this way by pressing Ctrl-C.

During development, it’s often useful to run paster serve using its --reload option. When --reload is passed to paster serve, changes to any Python module your project uses will cause the server to restart. This typically makes development easier, as changes to Python code made within a Pyramid application is not put into effect until the server restarts.

For example:

$ ../bin/paster serve development.ini --reload
Starting subprocess with file monitor
Starting server in PID 16601.
serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

For more detailed information about the startup process, see Startup. For more information about environment variables and configuration file settings that influence startup and runtime behavior, see Environment Variables and .ini File Settings.

Viewing the Application

Once your application is running via paster serve, you may visit http://localhost:6543/ in your browser. You will see something in your browser like what is displayed in the following image:

narr/project.png

This is the page shown by default when you visit an unmodified paster create -generated pyramid_starter application in a browser.

The Project Structure

The pyramid_starter scaffold generated a project (named MyProject), which contains a Python package. The package is also named myproject, but it’s lowercased; the scaffold generates a project which contains a package that shares its name except for case.

All Pyramid paster -generated projects share a similar structure. The MyProject project we’ve generated has the following directory structure:

MyProject/
|-- CHANGES.txt
|-- development.ini
|-- MANIFEST.in
|-- myproject
|   |-- __init__.py
|   |-- resources.py
|   |-- static
|   |   |-- favicon.ico
|   |   |-- logo.png
|   |   `-- pylons.css
|   |-- templates
|   |   `-- mytemplate.pt
|   |-- tests.py
|   `-- views.py
|-- production.ini
|-- README.txt
|-- setup.cfg
`-- setup.py

The MyProject Project

The MyProject project directory is the distribution and deployment wrapper for your application. It contains both the myproject package representing your application as well as files used to describe, run, and test your application.

  1. CHANGES.txt describes the changes you’ve made to the application. It is conventionally written in ReStructuredText format.
  2. README.txt describes the application in general. It is conventionally written in ReStructuredText format.
  3. development.ini is a PasteDeploy configuration file that can be used to execute your application during development.
  4. production.ini is a PasteDeploy configuration file that can be used to execute your application in a production configuration.
  5. setup.cfg is a setuptools configuration file used by setup.py.
  6. MANIFEST.in is a distutils “manifest” file, naming which files should be included in a source distribution of the package when python setup.py sdist is run.
  7. setup.py is the file you’ll use to test and distribute your application. It is a standard setuptools setup.py file.

development.ini

The development.ini file is a PasteDeploy configuration file. Its purpose is to specify an application to run when you invoke paster serve, as well as the deployment settings provided to that application.

The generated development.ini file looks like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[app:MyProject]
use = egg:MyProject
reload_templates = true
debug_authorization = false
debug_notfound = false
debug_routematch = false
debug_templates = true
default_locale_name = en

[pipeline:main]
pipeline =
    egg:WebError#evalerror
    MyProject

[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = 6543

# Begin logging configuration

[loggers]
keys = root, myproject

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_myproject]
level = DEBUG
handlers =
qualname = myproject

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s] %(message)s

# End logging configuration

This file contains several “sections” including [app:MyProject], [pipeline:main], [server:main] and several other sections related to logging configuration.

The [app:MyProject] section represents configuration for your application. This section name represents the MyProject application (and it’s an app -lication, thus app:MyProject)

The use setting is required in the [app:MyProject] section. The use setting points at a setuptools entry point named MyProject (the egg: prefix in egg:MyProject indicates that this is an entry point URI specifier, where the “scheme” is “egg”). egg:MyProject is actually shorthand for a longer spelling: egg:MyProject#main. The #main part is omitted for brevity, as it is the default.

The use setting is the only setting required in the [app:MyProject] section unless you’ve changed the callable referred to by the egg:MyProject entry point to accept more arguments: other settings you add to this section are passed as keywords arguments to the callable represented by this entry point (main in our __init__.py module). You can provide startup-time configuration parameters to your application by adding more settings to this section.

The reload_templates setting in the [app:MyProject] section is a Pyramid -specific setting which is passed into the framework. If it exists, and its value is true, Chameleon and Mako template changes will not require an application restart to be detected. See Automatically Reloading Templates for more information.

The debug_templates setting in the [app:MyProject] section is a Pyramid -specific setting which is passed into the framework. If it exists, and its value is true, Chameleon template exceptions will contained more detailed and helpful information about the error than when this value is false. See Nicer Exceptions in Chameleon Templates for more information.

Warning

The reload_templates and debug_templates options should be turned off for production applications, as template rendering is slowed when either is turned on.

Various other settings may exist in this section having to do with debugging or influencing runtime behavior of a Pyramid application. See Environment Variables and .ini File Settings for more information about these settings.

[pipeline:main], has the name main signifying that this is the default ‘application’ (although it’s actually a pipeline of middleware and an application) run by paster serve when it is invoked against this configuration file. The name main is a convention used by PasteDeploy signifying that it is the default application.

The [server:main] section of the configuration file configures a WSGI server which listens on TCP port 6543. It is configured to listen on all interfaces (0.0.0.0). The Paste#http server will create a new thread for each request.

Note

In general, Pyramid applications generated from scaffolds should be threading-aware. It is not required that a Pyramid application be nonblocking as all application code will run in its own thread, provided by the server you’re using.

The sections that live between the markers # Begin logging configuration and # End logging configuration represent Python’s standard library logging module configuration for your application. The sections between these two markers are passed to the logging module’s config file configuration engine when the paster serve or paster pshell commands are executed. The default configuration sends application logging output to the standard error output of your terminal.

See the PasteDeploy documentation for more information about other types of things you can put into this .ini file, such as other applications, middleware and alternate WSGI server implementations.

Note

You can add a [DEFAULT] section to your development.ini file. Such a section should consists of global parameters that are shared by all the applications, servers and middleware defined within the configuration file. The values in a [DEFAULT] section will be passed to your application’s main function as global_config (see the reference to the main function in __init__.py).

production.ini

The production.ini file is a PasteDeploy configuration file with a purpose much like that of development.ini. However, it disables the WebError interactive debugger, replacing it with a logger which outputs exception messages to stderr by default. It also turns off template development options such that templates are not automatically reloaded when changed, and turns off all debugging options. You can use this file instead of development.ini when you put your application into production.

MANIFEST.in

The MANIFEST.in file is a distutils configuration file which specifies the non-Python files that should be included when a distribution of your Pyramid project is created when you run python setup.py sdist. Due to the information contained in the default MANIFEST.in, an sdist of your Pyramid project will include .txt files, .ini files, .rst files, graphics files, and template files, as well as .py files. See http://docs.python.org/distutils/sourcedist.html#the-manifest-in-template for more information about the syntax and usage of MANIFEST.in.

setup.py

The setup.py file is a setuptools setup file. It is meant to be run directly from the command line to perform a variety of functions, such as testing your application, packaging, and distributing your application.

Note

setup.py is the defacto standard which Python developers use to distribute their reusable code. You can read more about setup.py files and their usage in the Setuptools documentation.

Our generated setup.py looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os

from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.txt')).read()
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()

requires = ['pyramid', 'WebError']

setup(name='MyProject',
      version='0.0',
      description='MyProject',
      long_description=README + '\n\n' +  CHANGES,
      classifiers=[
        "Programming Language :: Python",
        "Framework :: Pylons",
        "Topic :: Internet :: WWW/HTTP",
        "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
        ],
      author='',
      author_email='',
      url='',
      keywords='web pyramid pylons',
      packages=find_packages(),
      include_package_data=True,
      zip_safe=False,
      install_requires=requires,
      tests_require=requires,
      test_suite="myproject",
      entry_points = """\
      [paste.app_factory]
      main = myproject:main
      """,
      paster_plugins=['pyramid'],
      )

The setup.py file calls the setuptools setup function, which does various things depending on the arguments passed to setup.py on the command line.

Within the arguments to this function call, information about your application is kept. While it’s beyond the scope of this documentation to explain everything about setuptools setup files, we’ll provide a whirlwind tour of what exists in this file in this section.

Your application’s name can be any string; it is specified in the name field. The version number is specified in the version value. A short description is provided in the description field. The long_description is conventionally the content of the README and CHANGES file appended together. The classifiers field is a list of Trove classifiers describing your application. author and author_email are text fields which probably don’t need any description. url is a field that should point at your application project’s URL (if any). packages=find_packages() causes all packages within the project to be found when packaging the application. include_package_data will include non-Python files when the application is packaged if those files are checked into version control. zip_safe indicates that this package is not safe to use as a zipped egg; instead it will always unpack as a directory, which is more convenient. install_requires and tests_require indicate that this package depends on the pyramid package. test_suite points at the package for our application, which means all tests found in the package will be run when setup.py test is invoked. We examined entry_points in our discussion of the development.ini file; this file defines the main entry point that represents our project’s application.

Usually you only need to think about the contents of the setup.py file when distributing your application to other people, or when versioning your application for your own use. For fun, you can try this command now:

$ python setup.py sdist

This will create a tarball of your application in a dist subdirectory named MyProject-0.1.tar.gz. You can send this tarball to other people who want to use your application.

Warning

Without the presence of a MANIFEST.in file or without checking your source code into a version control repository, setup.py sdist places only Python source files (files ending with a .py extension) into tarballs generated by python setup.py sdist. This means, for example, if your project was not checked into a setuptools-compatible source control system, and your project directory didn’t contain a MANIFEST.in file that told the sdist machinery to include *.pt files, the myproject/templates/mytemplate.pt file would not be included in the generated tarball.

Projects generated by Pyramid scaffolds include a default MANIFEST.in file. The MANIFEST.in file contains declarations which tell it to include files like *.pt, *.css and *.js in the generated tarball. If you include files with extensions other than the files named in the project’s MANIFEST.in and you don’t make use of a setuptools-compatible version control system, you’ll need to edit the MANIFEST.in file and include the statements necessary to include your new files. See http://docs.python.org/distutils/sourcedist.html#principle for more information about how to do this.

You can also delete MANIFEST.in from your project and rely on a setuptools feature which simply causes all files checked into a version control system to be put into the generated tarball. To allow this to happen, check all the files that you’d like to be distributed along with your application’s Python files into Subversion. After you do this, when you rerun setup.py sdist, all files checked into the version control system will be included in the tarball. If you don’t use Subversion, and instead use a different version control system, you may need to install a setuptools add-on such as setuptools-git or setuptools-hg for this behavior to work properly.

setup.cfg

The setup.cfg file is a setuptools configuration file. It contains various settings related to testing and internationalization:

Our generated setup.cfg looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[nosetests]
match = ^test
nocapture = 1
cover-package = myproject
with-coverage = 1
cover-erase = 1

[compile_catalog]
directory = myproject/locale
domain = MyProject
statistics = true

[extract_messages]
add_comments = TRANSLATORS:
output_file = myproject/locale/MyProject.pot
width = 80

[init_catalog]
domain = MyProject
input_file = myproject/locale/MyProject.pot
output_dir = myproject/locale

[update_catalog]
domain = MyProject
input_file = myproject/locale/MyProject.pot
output_dir = myproject/locale
previous = true

The values in the default setup file allow various commonly-used internationalization commands and testing commands to work more smoothly.

The myproject Package

The myproject package lives inside the MyProject project. It contains:

  1. An __init__.py file signifies that this is a Python package. It also contains code that helps users run the application, including a main function which is used as a Paste entry point.
  2. A resources.py module, which contains resource code.
  3. A templates directory, which contains Chameleon (or other types of) templates.
  4. A tests.py module, which contains unit test code for the application.
  5. A views.py module, which contains view code for the application.

These are purely conventions established by the scaffold: Pyramid doesn’t insist that you name things in any particular way. However, it’s generally a good idea to follow Pyramid standards for naming, so that other Pyramid developers can get up to speed quickly on your code when you need help.

__init__.py

We need a small Python module that configures our application and which advertises an entry point for use by our PasteDeploy .ini file. This is the file named __init__.py. The presence of an __init__.py also informs Python that the directory which contains it is a package.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from pyramid.config import Configurator
from myproject.resources import Root

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(root_factory=Root, settings=settings)
    config.add_view('myproject.views.my_view',
                    context='myproject.resources.Root',
                    renderer='myproject:templates/mytemplate.pt')
    config.add_static_view('static', 'myproject:static')
    return config.make_wsgi_app()
  1. Line 1 imports the Configurator class from pyramid.config that we use later.

  2. Line 2 imports the Root class from myproject.resources that we use later.

  3. Lines 4-12 define a function named main that returns a Pyramid WSGI application. This function is meant to be called by the PasteDeploy framework as a result of running paster serve.

    Within this function, application configuration is performed.

    Lines 8-10 register a “default view” (a view that has no name attribute). It is registered so that it will be found when the context of the request is an instance of the myproject.resources.Root class. The first argument to add_view points at a Python function that does all the work for this view, also known as a view callable, via a dotted Python name. The view declaration also names a renderer, which in this case is a template that will be used to render the result of the view callable. This particular view declaration points at myproject:templates/mytemplate.pt, which is a asset specification that specifies the mytemplate.pt file within the templates directory of the myproject package. The template file it actually points to is a Chameleon ZPT template file.

    Line 11 registers a static view, which will serve up the files from the mypackage:static asset specification (the static directory of the mypackage package).

    Line 12 returns a WSGI application to the caller of the function (Paste).

views.py

Much of the heavy lifting in a Pyramid application is done by view callables. A view callable is the main tool of a Pyramid web application developer; it is a bit of code which accepts a request and which returns a response.

1
2
def my_view(request):
    return {'project':'MyProject'}

This bit of code was registered as the view callable within __init__.py (via add_view). add_view said that the default URL for instances that are of the class myproject.resources.Root should run this myproject.views.my_view() function.

This view callable function is handed a single piece of information: the request. The request is an instance of the WebOb Request class representing the browser’s request to our server.

This view returns a dictionary. When this view is invoked, a renderer converts the dictionary returned by the view into HTML, and returns the result as the response. This view is configured to invoke a renderer which uses a Chameleon ZPT template (mypackage:templates/my_template.pt, as specified in the __init__.py file call to add_view).

See Writing View Callables Which Use a Renderer for more information about how views, renderers, and templates relate and cooperate.

Note

Because our development.ini has a reload_templates = true directive indicating that templates should be reloaded when they change, you won’t need to restart the application server to see changes you make to templates. During development, this is handy. If this directive had been false (or if the directive did not exist), you would need to restart the application server for each template change. For production applications, you should set your project’s reload_templates to false to increase the speed at which templates may be rendered.

resources.py

The resources.py module provides the resource data and behavior for our application. Resources are objects which exist to provide site structure in applications which use traversal to map URLs to code. We write a class named Root that provides the behavior for the root resource.

1
2
3
class Root(object):
    def __init__(self, request):
        self.request = request
  1. Lines 1-3 define the Root class. The Root class is a “root resource factory” function that will be called by the Pyramid Router for each request when it wants to find the root of the resource tree.

In a “real” application, the Root object would likely not be such a simple object. Instead, it might be an object that could access some persistent data store, such as a database. Pyramid doesn’t make any assumption about which sort of data storage you’ll want to use, so the sample application uses an instance of myproject.resources.Root to represent the root.

static

This directory contains static assets which support the mytemplate.pt template. It includes CSS and images.

templates/mytemplate.pt

The single Chameleon template exists in the project. Its contents are too long to show here, but it displays a default page when rendered. It is referenced by the call to add_view as the renderer attribute in the __init__ file. See Writing View Callables Which Use a Renderer for more information about renderers.

Templates are accessed and used by view configurations and sometimes by view functions themselves. See Using Templates Directly and Templates Used as Renderers via Configuration.

tests.py

The tests.py module includes unit tests for your application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import unittest

from pyramid import testing

class ViewTests(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def test_my_view(self):
        from myproject.views import my_view
        request = testing.DummyRequest()
        info = my_view(request)
        self.assertEqual(info['project'], 'MyProject')

This sample tests.py file has a single unit test defined within it. This test is executed when you run python setup.py test. You may add more tests here as you build your application. You are not required to write tests to use Pyramid, this file is simply provided as convenience and example.

See Unit, Integration, and Functional Testing for more information about writing Pyramid unit tests.

Modifying Package Structure

It is best practice for your application’s code layout to not stray too much from accepted Pyramid scaffold defaults. If you refrain from changing things very much, other Pyramid coders will be able to more quickly understand your application. However, the code layout choices made for you by a scaffold are in no way magical or required. Despite the choices made for you by any scaffold, you can decide to lay your code out any way you see fit.

For example, the configuration method named add_view() requires you to pass a dotted Python name or a direct object reference as the class or function to be used as a view. By default, the pyramid_starter scaffold would have you add view functions to the views.py module in your package. However, you might be more comfortable creating a views directory, and adding a single file for each view.

If your project package name was myproject and you wanted to arrange all your views in a Python subpackage within the myproject package named views instead of within a single views.py file, you might:

  • Create a views directory inside your mypackage package directory (the same directory which holds views.py).
  • Move the existing views.py file to a file inside the new views directory named, say, blog.py.
  • Create a file within the new views directory named __init__.py (it can be empty, this just tells Python that the views directory is a package.

Then change the __init__.py of your myproject project (not the __init__.py you just created in the views directory, the one in its parent directory). For example, from something like:

1
2
config.add_view('myproject.views.my_view',
                renderer='myproject:templates/mytemplate.pt')

To this:

1
2
config.add_view('myproject.views.blog.my_view',
                renderer='myproject:templates/mytemplate.pt')

You can then continue to add files to the views directory, and refer to view classes or functions within those files via the dotted name passed as the first argument to add_view. For example, if you added a file named anothermodule.py to the views subdirectory, and added a view callable named my_view to it:

1
2
config.add_view('myproject.views.anothermodule.my_view',
                renderer='myproject:templates/anothertemplate.pt')

This pattern can be used to rearrage code referred to by any Pyramid API argument which accepts a dotted Python name or direct object reference.

Using the Interactive Shell

It is possible to use a Python interpreter prompt loaded with a similar configuration as would be loaded if you were running your Pyramid application via paster serve. This can be a useful debugging tool. See The Interactive Shell for more details.