Use Pyramid's 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 setuptools 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.
First we copy the results of the previous step:
$ cd ..; cp -r package ini; cd ini
Our ini/setup.py needs a setuptools "entry point" in the setup() function:
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 ini/tutorial.egg-info:
$ $VENV/bin/python setup.py develop
Let's make a file ini/development.ini for our configuration:
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
[app:main] use = egg:tutorial [server:main] use = egg:pyramid#wsgiref host = 0.0.0.0 port = 6543 # Begin logging configuration [loggers] keys = root, tutorial [logger_tutorial] level = DEBUG handlers = qualname = tutorial [handlers] keys = console [formatters] keys = generic [logger_root] level = INFO handlers = console [handler_console] class = StreamHandler args = (sys.stderr,) level = NOTSET formatter = generic [formatter_generic] format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s # End logging configuration
We can refactor our startup code from the previous step's app.py into ini/tutorial/__init__.py:
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()
Now that ini/tutorial/app.py isn't used, let's remove it:
$ rm tutorial/app.py
Run your Pyramid application with:
$ $VENV/bin/pserve development.ini --reload
Our development.ini file is read by pserve and serves to bootstrap our application. Processing then proceeds as described in the Pyramid chapter on application startup:
The .ini file is also used for two other functions:
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 __init__.py.
The 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.