Search code examples
pythondev-to-production

Toggle between production and development


I have a global flag in one of my modules, config/top.py:

RUNNING_MODE = "production"  # could also be "development", set manually

Depending on this flag, I would like to include some production/development settings. For example, in production.py I want to have:

LOG_LEVEL = "WARNING"

And in development.py:

LOG_LEVEL = "INFO"

(there are much more settings to be set)

The goal is to be able to use those settings transparently in any of my modules, let's say test.py:

from config.settings import LOG_LEVEL

This would use the right setting, either from production.py or from development.py, depending on RUNNING_MODE.

Is there any accepted approach to handle this kind of setup? How would I structure the directories/modules so that, just by changing the RUNNING_MODE in config/top.py the whole configuration happens transparently?

Note: I prefer not to have this in the build process, but to have it embedded in the module structure. That is, I do not want the build process to modify any of my modules.


Solution

  • You can easily achieve this.

    Following is the folder structure:

    > config/
        settings.py
        production.py
        development.py
    

    Now in settings.py:

    RUNNING_MODE = "production"  # could also be "development", set manually
    
    if ENVIRONMENT == "production":
        from production import *
    elif ENVIRONMENT == "development":
        from development import *
    

    Keep all your environment dependent settings in individual files.

    And then, in views.py or any other file in project.

    from config.settings import LOG_LEVEL