jeudi 16 juin 2016

Update a python module namespace with another

I am working on a python application which will run in different environments (dev box, int, prod). I have some settings and configuration in a python file - settings.py. Apart from that, for each environment, I plan to have a separate settings file (say settings-dev.py for my dev environment).

Based on the used specified command line argument for the environment, I want the appropriate environment specific setting file to be imported. However, i want the imported settings file (say - settings-dev.py) to update the namespace for my common settings file - such that the rest of my modules just import and use settings from the settings.py file. Note that the environment specific file may only override (or introduce) some settings in which case the original settings in settings.py should still be available for the modules importing settings.py.

Is there any way to update the namespace of settings.py with that of another settings file such that the importer gets the updated values from the merged settings? (behavior desired is similar to the one exposed by Django.conf.settings).

Also, is there a better recommendation to achieve this? I could also rely on some set environment variables such that my single settings.py bears all the values and the right ones get picked based on environment variables being set. However, that was making the settings.py file too ugly so was exploring this alternative.

Aucun commentaire:

Enregistrer un commentaire