# Installing a plug-in
pip install dynaconf
# Initialize configuration file : establish config Folder ,cd config
dynaconf init -f yaml
.secret.yaml Configure private information , Important passwords can be put here ,git Ignore without passing it to the library
(1) stay config.py Enable the layering function in environment=True,
import os
from dynaconf import Dynaconf
sys_env = os.environ.get('ENV_FOR_DYNACONF')
if sys_env:
env = sys_env
else:
env = 'DEFAULT'
config = Dynaconf(envvar_prefix="BOOKSTORE_WEB_CONF",
settings_files=['configs/settings.yaml'],
environments=True,
env=env) # env Specify the priority configuration layer , If this layer is not found during the call, it will go to the first layer to find
(2)settings.yaml Specific layers are listed in , And the name :DEFAULT Layer is the default layer , I can't find object { VAR1 It's a whole , Find... In units of the entire object } Will come here to find , Other layers can be customized : Each time you search the configuration, first specify this layer to search , If you can't find it, go to the default layer
DEFAULT:
NAME: 'DEFAULT'
VAR1:
KEY: 1
NAME: ' test '
VAR2:
KEY: 3
DEVELOPMENT:
NAME: 'DEVELOPMENT'
VAR1:
KEY: 2
PRODUCTION:
NAME: 'PRODUCTION'
Be careful :config.py After layering is turned on in ,settings.yaml There must be a hierarchy in
#export ENV_FOR_DYNACONF=DEFAULT
export ENV_FOR_DYNACONF=DEVELOPMENT
Add configuration and update system variables ,source ~/.bashrc