Python Module Configuration

Google App Engine provides a mechanism allowing users to specify their own values for constants and “hook functions” for use by some App Engine modules. Specifying your own values can change the default behavior of those modules based on the application's needs. The file where you specify these constants is After creating this file, you simply deploy it with your other code.

  1. Python Module Configuration with
  2. Configurable App Engine Modules
  3. Configuring Your Own Modules with lib_config

Python Module Configuration with

Several App Engine modules are configurable using

To customize the modules, create a new file in your application's root directory. To use this file, you need to define only those constants or hook functions you wish to override. After creating this file, run update to deploy it. The constants and/or hook functions will then be used by those modules internally.

To override a constant, prefix the constant's name with the module name and an underscore, then assign a value. For example, to edit overrides in appstats, you can define the value of KEY_PREFIX

appstats_KEY_PREFIX = '__my_custom_prefix__'

Naming of overridden hook functions is similar in other modules. For example, in namespace_manager, you can override the hook function default_namespace_for_request in as follows:

import os
def namespace_manager_default_namespace_for_request():
    return os.environ.get('HTTP_HOST', '')

Configurable App Engine Modules

The modules listed below are configurable using By convention, hook functions are lowercase and constants are uppercase:

webapp (For advanced users. Not available in Python 2.7)


  • default_namespace_for_request() (default returns None)



  • BASE_PATH (default '/_ah/datastore_admin')
  • MAPREDUCE_PATH (default '/_ah/mapreduce')


Configuring Your Own Modules with lib_config

App Engine also allows you to configure your own modules with constants and hook functions defined in The lib_config.register() function allows you to both register the names of the user-overridable constants and hooks, and to define sensible defaults in case the users don't wish to override them. Internally, lib_config.register() attempts to import appengine_config. If successful, it replaces the specified module's defaults with those defined in

Example usage in

from google.appengine.api import lib_config

def _hook_function1_default():
   return 'baz'

_config = lib_config.register('my_module', {'CONSTANT1': 'foo',
                                            'CONSTANT2': 'bar',
                                            'hook_function1': _hook_function1_default})

Now you can access a user's constants as


and call their hook function as


Some programmers like to group their defaults into a class, e.g.

class _ConfigDefaults(object):
  CONSTANT1 = 'foo'
  CONSTANT2 = 'bar'
  def hook_function1():
      return 'baz'

_config = lib_config.register('my_module',  _ConfigDefaults.__dict__)

In order to override your defaults, a user could define (in

my_module_CONSTANT1 = 'foofoo'
my_module_hook_function1 = lambda: 'bazbaz'

As a result, in, the following will be true:

  • _config.CONSTANT1 is now 'foofoo'
  • _config.CONSTANT2 remains 'bar'
  • _config.hook_function1() returns 'bazbaz'

The user overrides are available to immediately after lib_config.register() returns.

Send feedback about...