03: Application Configuration with
pserve command with a
.ini configuration file for
simpler, better application running.
Pyramid has a first-class concept of configuration
distinct from code. This approach is optional, but its presence makes it
distinct from other Python web frameworks. It taps into Python's
library, which establishes conventions for installing and providing "entry
points" for Python projects. Pyramid uses an entry point to let a Pyramid
application know where to find the WSGI app.
- Modify our
setup.pyto have an entry point telling Pyramid the location of the WSGI app.
- Create an application driven by an
- Start the application with Pyramid's
- Move code into the package's
First we copy the results of the previous step:
$ cd ..; cp -r package ini; cd ini
ini/setup.pyneeds a setuptools "entry point" in the
1 2 3 4 5 6 7 8 9 10 11 12 13
from setuptools import setup requires = [ 'pyramid', ] setup(name='tutorial', install_requires=requires, entry_points="""\ [paste.app_factory] main = tutorial:main """, )
We can now install our project, thus generating (or re-generating) an "egg" at
$ $VENV/bin/pip install -e .
Let's make a file
ini/development.inifor our configuration:
1 2 3 4 5 6 7
[app:main] use = egg:tutorial [server:main] use = egg:pyramid#wsgiref host = 0.0.0.0 port = 6543
We can refactor our startup code from the previous step's
1 2 3 4 5 6 7 8 9 10 11 12 13
from pyramid.config import Configurator from pyramid.response import Response def hello_world(request): return Response('<body><h1>Hello World!</h1></body>') def main(global_config, **settings): config = Configurator(settings=settings) config.add_route('hello', '/') config.add_view(hello_world, route_name='hello') return config.make_wsgi_app()
ini/tutorial/app.pyisn't used, let's remove it:
$ rm tutorial/app.py
Run your Pyramid application with:
$ $VENV/bin/pserve development.ini --reload
development.ini file is read by
pserve and serves to bootstrap our
application. Processing then proceeds as described in the Pyramid chapter on
use = egg:tutorial.
- The projects's
setup.pyhas defined an "entry point" (lines 9-12) for the project's "main" entry point of
- This function is invoked, with the values from certain
.inisections passed in.
.ini file is also used for two other functions:
- Configuring the WSGI server.
[server:main]wires up the choice of which WSGI server for your WSGI application. In this case, we are using
wsgirefbundled in the Python library. It also wires up the port number:
port = 6543tells
wsgirefto listen on port 6543.
- Configuring Python logging. Pyramid uses Python standard logging, which
needs a number of configuration values. The
.iniserves this function. This provides the console log output that you see on startup and each request.
We moved our startup code from
app.py to the package's
tutorial/__init__.py. This isn't necessary, but it is a common style in
Pyramid to take the WSGI app bootstrapping out of your module's code and put it
in the package's
pserve application runner has a number of command-line arguments and
options. We are using
--reload which tells
pserve to watch the
filesystem for changes to relevant code (Python files, the INI file, etc.) and,
when something changes, restart the application. Very handy during development.
- If you don't like configuration and/or
.inifiles, could you do this yourself in Python code?
- Can we have multiple
.iniconfiguration files for a project? Why might you want to do that?
- The entry point in
__init__.pywhen it declared
tutorial:mainfunction. Why not?
- What is the purpose of
**settings? What does the