How to get Registry().settings during Pyramid app startup time? How to get Registry().settings during Pyramid app startup time? python python

How to get Registry().settings during Pyramid app startup time?


Another option, if you enjoy global configuration via Python, create a settings.py file. If it needs values from the ini file, parse the ini file and grab them out (at module scope, so it runs at import time):

from paste.deploy.loadwsgi import appconfigconfig = appconfig('config:development.ini', 'myapp', relative_to='.')if config['env'] == 'production':    api_endpoint_uri = 'http://api.external.com/?{0}'    timezone = timezone('US/Eastern')# .. and so on ...

'config:development.ini' is the name of the ini file (prefixed with 'config:'). 'myapp' is the section name in the config file representing your app (e.g. [app:myapp]). "relative_to" is the directory name in which the config file can be found.


The pattern that I use is to pass the Configurator to modules that need to be initialized. Pyramid doesn't use any global variables because a design goal is to be able to run multiple instances of Pyramid in the same process. The threadlocals are global, but they are local to the current request, so different Pyramid apps can push to them at the same time from different threads.

With this in mind, if you do want a global settings dictionary you'll have to take care of that yourself. You could even push the registry onto the threadlocal manager yourself by calling config.begin().

I think the major thing to take away here is that you shouldn't be calling get_current_registry() at the module level, because at the time of import you aren't really guaranteed that the threadlocals are initialized, however in your init_model() function if you call get_current_registry(), you'd be fine if you previously called config.begin().

Sorry this is a little convoluted, but it's a common question and the best answer is: pass the configurator to your submodules that need it and allow them to add stuff to the registry/settings objects for use later.


Pyramid uses static configration by PasteDeploy, unlike Django.Your [EDIT] part is a nice solution, I think Pyramid community should consider such usage.