This article describes how to configure an app in the Python web framework. the code is based on Python2.x. For more information, see the Web framework and ORM framework, we can start assembling the App.
Generally, a Web App needs to read the configuration file when running, such as the database username and password, and run it in different environments, the Web App can obtain the correct configuration by reading different configuration files.
Because Python syntax is simple, you can directly use Python source code for configuration without parsing a separate configuration file, such as. properties or. yaml.
The default configuration file should fully comply with the local development environment, so that the server can be started immediately without any settings.
We name the default configuration file config_default.py:
# config_default.pyconfigs = { 'db': { 'host': '127.0.0.1', 'port': 3306, 'user': 'www-data', 'password': 'www-data', 'database': 'awesome' }, 'session': { 'secret': 'AwEsOmE' }}
The preceding configuration file is simple and clear. However, if you want to deploy the service to the server, you usually need to modify the host and other information of the database. modifying config_default.py directly is not a good solution. a better way is to write config_override.py to overwrite some default settings:
# config_override.pyconfigs = { 'db': { 'host': '192.168.0.100' }}
Using config_default.py as the standard configuration of the development environment and config_override.py as the standard configuration of the production environment, we can conveniently develop the application locally and deploy the application on the server at any time.
The application must first read the configuration file from config_override.py. To simplify the reading of the configuration file, you can read all the configurations to the unified config. py:
# config.pyconfigs = config_default.configstry: import config_override configs = merge(configs, config_override.configs)except ImportError: pass
In this way, the App configuration is completed.