1.. index::
2   single: environment variables
3   single: settings
4   single: reload
5   single: debug_authorization
6   single: reload_assets
7   single: debug_notfound
8   single: debug_all
9   single: reload_all
10   single: debug settings
11   single: debug_routematch
12   single: prevent_http_cache
13   single: reload settings
14   single: default_locale_name
15   single: environment variables
16   single: ini file settings
17   single: PasteDeploy settings
18
19.. _environment_chapter:
20
21Environment Variables and ``.ini`` File Settings
22================================================
23
24:app:`Pyramid` behavior can be configured through a combination of operating
25system environment variables and ``.ini`` configuration file application
26section settings.  The meaning of the environment variables and the
27configuration file settings overlap.
28
29.. note::
30  Where a configuration file setting exists with the same meaning as an
31  environment variable, and both are present at application startup time, the
32  environment variable setting takes precedence.
33
34The term "configuration file setting name" refers to a key in the ``.ini``
35configuration for your application.  The configuration file setting names
36documented in this chapter are reserved for :app:`Pyramid` use.  You should not
37use them to indicate application-specific configuration settings.
38
39Reloading Templates
40-------------------
41
42When this value is true, templates are automatically reloaded whenever they are
43modified without restarting the application, so you can see changes to
44templates take effect immediately during development.  This flag is meaningful
45to Chameleon and Mako templates, as well as most third-party template rendering
46extensions.
47
48+-------------------------------+--------------------------------+
49| Environment Variable Name     | Config File Setting Name       |
50+===============================+================================+
51| ``PYRAMID_RELOAD_TEMPLATES``  |  ``pyramid.reload_templates``  |
52|                               |   or ``reload_templates``      |
53+-------------------------------+--------------------------------+
54
55Reloading Assets
56----------------
57
58Don't cache any asset file data when this value is true.
59
60.. seealso::
61
62    See also :ref:`overriding_assets_section`.
63
64+----------------------------+-----------------------------+
65| Environment Variable Name  | Config File Setting Name    |
66+============================+=============================+
67| ``PYRAMID_RELOAD_ASSETS``  |  ``pyramid.reload_assets``  |
68|                            |  or ``reload_assets``       |
69+----------------------------+-----------------------------+
70
71.. note:: For backwards compatibility purposes, aliases can be used for
72   configuring asset reloading: ``PYRAMID_RELOAD_RESOURCES`` (envvar) and
73   ``pyramid.reload_resources`` (config file).
74
75Debugging Authorization
76-----------------------
77
78Print view authorization failure and success information to stderr when this
79value is true.
80
81.. seealso::
82
83    See also :ref:`debug_authorization_section`.
84
85+---------------------------------+-----------------------------------+
86| Environment Variable Name       | Config File Setting Name          |
87+=================================+===================================+
88| ``PYRAMID_DEBUG_AUTHORIZATION`` |  ``pyramid.debug_authorization``  |
89|                                 |  or ``debug_authorization``       |
90+---------------------------------+-----------------------------------+
91
92Debugging Not Found Errors
93--------------------------
94
95Print view-related ``NotFound`` debug messages to stderr when this value is
96true.
97
98.. seealso::
99
100    See also :ref:`debug_notfound_section`.
101
102+----------------------------+------------------------------+
103| Environment Variable Name  | Config File Setting Name     |
104+============================+==============================+
105| ``PYRAMID_DEBUG_NOTFOUND`` |  ``pyramid.debug_notfound``  |
106|                            |  or ``debug_notfound``       |
107+----------------------------+------------------------------+
108
109Debugging Route Matching
110------------------------
111
112Print debugging messages related to :term:`url dispatch` route matching when
113this value is true.
114
115.. seealso::
116
117    See also :ref:`debug_routematch_section`.
118
119+------------------------------+--------------------------------+
120| Environment Variable Name    | Config File Setting Name       |
121+==============================+================================+
122| ``PYRAMID_DEBUG_ROUTEMATCH`` |  ``pyramid.debug_routematch``  |
123|                              |  or ``debug_routematch``       |
124+------------------------------+--------------------------------+
125
126.. _preventing_http_caching:
127
128Preventing HTTP Caching
129-----------------------
130
131Prevent the ``http_cache`` view configuration argument from having any effect
132globally in this process when this value is true.  No HTTP caching-related
133response headers will be set by the :app:`Pyramid` ``http_cache`` view
134configuration feature when this is true.
135
136.. seealso::
137
138    See also :ref:`influencing_http_caching`.
139
140+---------------------------------+----------------------------------+
141| Environment Variable Name       | Config File Setting Name         |
142+=================================+==================================+
143| ``PYRAMID_PREVENT_HTTP_CACHE``  |  ``pyramid.prevent_http_cache``  |
144|                                 |  or ``prevent_http_cache``       |
145+---------------------------------+----------------------------------+
146
147Preventing Cache Busting
148------------------------
149
150Prevent the ``cachebust`` static view configuration argument from having any
151effect globally in this process when this value is true.  No cache buster will
152be configured or used when this is true.
153
154.. versionadded:: 1.6
155
156.. seealso::
157
158    See also :ref:`cache_busting`.
159
160+---------------------------------+----------------------------------+
161| Environment Variable Name       | Config File Setting Name         |
162+=================================+==================================+
163| ``PYRAMID_PREVENT_CACHEBUST``   |  ``pyramid.prevent_cachebust``   |
164|                                 |  or ``prevent_cachebust``        |
165+---------------------------------+----------------------------------+
166
167Debugging All
168-------------
169
170Turns on all ``debug*`` settings.
171
172+----------------------------+---------------------------+
173| Environment Variable Name  | Config File Setting Name  |
174+============================+===========================+
175| ``PYRAMID_DEBUG_ALL``      |  ``pyramid.debug_all``    |
176|                            |  or ``debug_all``         |
177+----------------------------+---------------------------+
178
179Reloading All
180-------------
181
182Turns on all ``reload*`` settings.
183
184+---------------------------+----------------------------+
185| Environment Variable Name | Config File Setting Name   |
186+===========================+============================+
187| ``PYRAMID_RELOAD_ALL``    |  ``pyramid.reload_all`` or |
188|                           |  ``reload_all``            |
189+---------------------------+----------------------------+
190
191.. _default_locale_name_setting:
192
193Default Locale Name
194-------------------
195
196The value supplied here is used as the default locale name when a :term:`locale
197negotiator` is not registered.
198
199.. seealso::
200
201    See also :ref:`localization_deployment_settings`.
202
203+---------------------------------+-----------------------------------+
204| Environment Variable Name       | Config File Setting Name          |
205+=================================+===================================+
206| ``PYRAMID_DEFAULT_LOCALE_NAME`` |  ``pyramid.default_locale_name``  |
207|                                 |  or ``default_locale_name``       |
208+---------------------------------+-----------------------------------+
209
210.. _including_packages:
211
212Including Packages
213------------------
214
215``pyramid.includes`` instructs your application to include other packages.
216Using the setting is equivalent to using the
217:meth:`pyramid.config.Configurator.include` method.
218
219+--------------------------+
220| Config File Setting Name |
221+==========================+
222| ``pyramid.includes``     |
223+--------------------------+
224
225The value assigned to ``pyramid.includes`` should be a sequence.  The sequence
226can take several different forms.
227
2281) It can be a string.
229
230   If it is a string, the package names can be separated by spaces::
231
232      package1 package2 package3
233
234   The package names can also be separated by carriage returns::
235
236       package1
237       package2
238       package3
239
2402) It can be a Python list, where the values are strings::
241
242   ['package1', 'package2', 'package3']
243
244Each value in the sequence should be a :term:`dotted Python name`.
245
246``pyramid.includes`` vs. :meth:`pyramid.config.Configurator.include`
247~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
248
249Two methods exist for including packages: ``pyramid.includes`` and
250:meth:`pyramid.config.Configurator.include`.  This section explains their
251equivalence.
252
253Using PasteDeploy
254+++++++++++++++++
255
256Using the following ``pyramid.includes`` setting in the PasteDeploy ``.ini``
257file in your application:
258
259.. code-block:: ini
260
261   [app:main]
262   pyramid.includes = pyramid_debugtoolbar
263                      pyramid_tm
264
265Is equivalent to using the following statements in your configuration code:
266
267.. code-block:: python
268   :linenos:
269
270   from pyramid.config import Configurator
271
272   def main(global_config, **settings):
273       config = Configurator(settings=settings)
274       # ...
275       config.include('pyramid_debugtoolbar')
276       config.include('pyramid_tm')
277       # ...
278
279It is fine to use both or either form.
280
281Plain Python
282++++++++++++
283
284Using the following ``pyramid.includes`` setting in your plain-Python Pyramid
285application:
286
287.. code-block:: python
288   :linenos:
289
290   from pyramid.config import Configurator
291
292   if __name__ == '__main__':
293       settings = {'pyramid.includes':'pyramid_debugtoolbar pyramid_tm'}
294       config = Configurator(settings=settings)
295
296Is equivalent to using the following statements in your configuration code:
297
298.. code-block:: python
299   :linenos:
300
301   from pyramid.config import Configurator
302
303   if __name__ == '__main__':
304       settings = {}
305       config = Configurator(settings=settings)
306       config.include('pyramid_debugtoolbar')
307       config.include('pyramid_tm')
308
309It is fine to use both or either form.
310
311.. _explicit_tween_config:
312
313Explicit Tween Configuration
314----------------------------
315
316This value allows you to perform explicit :term:`tween` ordering in your
317configuration.  Tweens are bits of code used by add-on authors to extend
318Pyramid.  They form a chain, and require ordering.
319
320Ideally you won't need to use the ``pyramid.tweens`` setting at all.  Tweens
321are generally ordered and included "implicitly" when an add-on package which
322registers a tween is "included".  Packages are included when you name a
323``pyramid.includes`` setting in your configuration or when you call
324:meth:`pyramid.config.Configurator.include`.
325
326Authors of included add-ons provide "implicit" tween configuration ordering
327hints to Pyramid when their packages are included.  However, the implicit tween
328ordering is only best-effort.  Pyramid will attempt to provide an implicit
329order of tweens as best it can using hints provided by add-on authors, but
330because it's only best-effort, if very precise tween ordering is required, the
331only surefire way to get it is to use an explicit tween order. You may be
332required to inspect your tween ordering (see :ref:`displaying_tweens`) and add
333a ``pyramid.tweens`` configuration value at the behest of an add-on author.
334
335+---------------------------+
336| Config File Setting Name  |
337+===========================+
338| ``pyramid.tweens``        |
339+---------------------------+
340
341The value assigned to ``pyramid.tweens`` should be a sequence.  The sequence
342can take several different forms.
343
3441) It can be a string.
345
346   If it is a string, the tween names can be separated by spaces::
347
348      pkg.tween_factory1 pkg.tween_factory2 pkg.tween_factory3
349
350   The tween names can also be separated by carriage returns::
351
352      pkg.tween_factory1
353      pkg.tween_factory2
354      pkg.tween_factory3
355
3562) It can be a Python list, where the values are strings::
357
358   ['pkg.tween_factory1', 'pkg.tween_factory2', 'pkg.tween_factory3']
359
360Each value in the sequence should be a :term:`dotted Python name`.
361
362PasteDeploy Configuration vs. Plain-Python Configuration
363~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
364
365Using the following ``pyramid.tweens`` setting in the PasteDeploy ``.ini`` file
366in your application:
367
368.. code-block:: ini
369
370   [app:main]
371   pyramid.tweens = pyramid_debugtoolbar.toolbar.tween_factory
372                    pyramid.tweens.excview_tween_factory
373                    pyramid_tm.tm_tween_factory
374
375Is equivalent to using the following statements in your configuration code:
376
377.. code-block:: python
378   :linenos:
379
380   from pyramid.config import Configurator
381
382   def main(global_config, **settings):
383       settings['pyramid.tweens'] = [
384               'pyramid_debugtoolbar.toolbar.tween_factory',
385               'pyramid.tweebs.excview_tween_factory',
386               'pyramid_tm.tm_tween_factory',
387                ]
388       config = Configurator(settings=settings)
389
390It is fine to use both or either form.
391
392Examples
393--------
394
395Let's presume your configuration file is named ``MyProject.ini``, and there is
396a section representing your application named ``[app:main]`` within the file
397that represents your :app:`Pyramid` application. The configuration file
398settings documented in the above "Config File Setting Name" column would go in
399the ``[app:main]`` section.  Here's an example of such a section:
400
401.. code-block:: ini
402  :linenos:
403
404  [app:main]
405  use = egg:MyProject
406  pyramid.reload_templates = true
407  pyramid.debug_authorization = true
408
409You can also use environment variables to accomplish the same purpose for
410settings documented as such.  For example, you might start your :app:`Pyramid`
411application using the following command line:
412
413.. code-block:: text
414
415  $ PYRAMID_DEBUG_AUTHORIZATION=1 PYRAMID_RELOAD_TEMPLATES=1 \
416         $VENV/bin/pserve MyProject.ini
417
418If you started your application this way, your :app:`Pyramid` application would
419behave in the same manner as if you had placed the respective settings in the
420``[app:main]`` section of your application's ``.ini`` file.
421
422If you want to turn all ``debug`` settings (every setting that starts with
423``pyramid.debug_``) on in one fell swoop, you can use ``PYRAMID_DEBUG_ALL=1``
424as an environment variable setting or you may use ``pyramid.debug_all=true`` in
425the config file.  Note that this does not affect settings that do not start
426with ``pyramid.debug_*`` such as ``pyramid.reload_templates``.
427
428If you want to turn all ``pyramid.reload`` settings (every setting that starts
429with ``pyramid.reload_``) on in one fell swoop, you can use
430``PYRAMID_RELOAD_ALL=1`` as an environment variable setting or you may use
431``pyramid.reload_all=true`` in the config file.  Note that this does not affect
432settings that do not start with ``pyramid.reload_*`` such as
433``pyramid.debug_notfound``.
434
435.. note::
436   Specifying configuration settings via environment variables is generally
437   most useful during development, where you may wish to augment or override
438   the more permanent settings in the configuration file. This is useful
439   because many of the reload and debug settings may have performance or
440   security (i.e., disclosure) implications that make them undesirable in a
441   production environment.
442
443.. index::
444   single: reload_templates
445   single: reload_assets
446
447Understanding the Distinction Between ``reload_templates`` and ``reload_assets``
448--------------------------------------------------------------------------------
449
450The difference between ``pyramid.reload_assets`` and
451``pyramid.reload_templates`` is a bit subtle. Templates are themselves also
452treated by :app:`Pyramid` as asset files (along with other static files), so
453the distinction can be confusing.  It's helpful to read
454:ref:`overriding_assets_section` for some context about assets in general.
455
456When ``pyramid.reload_templates`` is true, :app:`Pyramid` takes advantage of
457the underlying templating system's ability to check for file modifications to
458an individual template file.  When ``pyramid.reload_templates`` is true, but
459``pyramid.reload_assets`` is *not* true, the template filename returned by the
460``pkg_resources`` package (used under the hood by asset resolution) is cached
461by :app:`Pyramid` on the first request.  Subsequent requests for the same
462template file will return a cached template filename.  The underlying
463templating system checks for modifications to this particular file for every
464request.  Setting ``pyramid.reload_templates`` to ``True`` doesn't affect
465performance dramatically (although it should still not be used in production
466because it has some effect).
467
468However, when ``pyramid.reload_assets`` is true, :app:`Pyramid` will not cache
469the template filename, meaning you can see the effect of changing the content
470of an overridden asset directory for templates without restarting the server
471after every change.  Subsequent requests for the same template file may return
472different filenames based on the current state of overridden asset directories.
473Setting ``pyramid.reload_assets`` to ``True`` affects performance
474*dramatically*, slowing things down by an order of magnitude for each template
475rendering.  However, it's convenient to enable when moving files around in
476overridden asset directories. ``pyramid.reload_assets`` makes the system *very
477slow* when templates are in use.  Never set ``pyramid.reload_assets`` to
478``True`` on a production system.
479
480.. index::
481   par: settings; adding custom
482
483.. _adding_a_custom_setting:
484
485Adding a Custom Setting
486-----------------------
487
488From time to time, you may need to add a custom setting to your application.
489Here's how:
490
491- If you're using an ``.ini`` file, change the ``.ini`` file, adding the
492  setting to the ``[app:foo]`` section representing your Pyramid application.
493  For example:
494
495  .. code-block:: ini
496
497    [app:main]
498    # .. other settings
499    debug_frobnosticator = True
500
501- In the ``main()`` function that represents the place that your Pyramid WSGI
502  application is created, anticipate that you'll be getting this key/value pair
503  as a setting and do any type conversion necessary.
504
505  If you've done any type conversion of your custom value, reset the converted
506  values into the ``settings`` dictionary *before* you pass the dictionary as
507  ``settings`` to the :term:`Configurator`.  For example:
508
509  .. code-block:: python
510
511     def main(global_config, **settings):
512         # ...
513         from pyramid.settings import asbool
514         debug_frobnosticator = asbool(settings.get(
515                    'debug_frobnosticator', 'false'))
516         settings['debug_frobnosticator'] = debug_frobnosticator
517         config = Configurator(settings=settings)
518
519  .. note::
520     It's especially important that you mutate the ``settings`` dictionary with
521     the converted version of the variable *before* passing it to the
522     Configurator: the configurator makes a *copy* of ``settings``, it doesn't
523     use the one you pass directly.
524
525-  When creating an ``includeme`` function that will be later added to your
526   application's configuration you may access the ``settings`` dictionary
527   through the instance of the :term:`Configurator` that is passed into the
528   function as its only argument.  For Example:
529
530  .. code-block:: python
531
532     def includeme(config):
533         settings = config.registry.settings
534         debug_frobnosticator = settings['debug_frobnosticator']
535
536- In the runtime code from where you need to access the new settings value,
537  find the value in the ``registry.settings`` dictionary and use it.  In
538  :term:`view` code (or any other code that has access to the request), the
539  easiest way to do this is via ``request.registry.settings``.  For example:
540
541  .. code-block:: python
542
543     settings = request.registry.settings
544     debug_frobnosticator = settings['debug_frobnosticator']
545
546  If you wish to use the value in code that does not have access to the request
547  and you wish to use the value, you'll need to use the
548  :func:`pyramid.threadlocal.get_current_registry` API to obtain the current
549  registry, then ask for its ``settings`` attribute.  For example:
550
551  .. code-block:: python
552
553     registry = pyramid.threadlocal.get_current_registry()
554     settings = registry.settings
555     debug_frobnosticator = settings['debug_frobnosticator']
556