Tutorials for configuring apps in Python's web framework

Source: Internet
Author: User
With the web framework and ORM Framework, we can start assembling apps.

Typically, a web app needs to read a configuration file at run time, such as a database username, password, etc., while running in a different environment, the web app can read different configuration files to get the right configuration.

Because of its simple syntax, Python can be configured directly with the Python source code without having to parse a separate. Properties or. yaml configuration file.

The default configuration file should be fully compliant with the local development environment so that you can start the server immediately without any setup.

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 above configuration file is simple and straightforward. However, if you want to deploy to the server, usually need to modify the database host and other information, directly modify config_default.py is not a good way, a better way is to write a config_override.py, to override some of the default settings:

# config_override.pyconfigs = {  ' db ': {    ' host ': ' 192.168.0.100 '  }}

Using config_default.py as the standard configuration for the development environment and config_override.py as a standard configuration for production environments, we can easily develop locally and deploy applications to servers at any time.

The application read configuration file needs to be read first from config_override.py. To simplify reading the configuration file, all configurations can be read into the unified config.py:

# config.pyconfigs = config_default.configstry:  import config_override  configs = Merge (Configs, Config_ Override.configs) except Importerror:  Pass

In this way, we have completed the configuration of the app.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.