You can import and use prettyconf
in your Python code:
from prettyconf import config
MY_CONFIG = config("PROJECT_MY_CONFIG")
If PROJECT_MY_CONFIG
is not defined in an environment variable neither in a .env
(or *.cfg
) file, prettyconf
will raise a UnknownConfiguration
exception.
Warning
prettyconf
will skip configuration files inside .zip
, .egg
or wheel packages.
In these cases you could define a default configuration value:
MY_CONFIG = config("PROJECT_MY_CONFIG", default="default value")
You can also use the cast
argument to convert a string value into a specific value type:
DEBUG = config("DEBUG", default=False, cast=config.boolean)
The boolean
cast converts strings values like On|Off
, 1|0
, yes|no
, true|False
into Python boolean True
or False
.
Find out more about other casts or how to write your own at Casts<casts>
.
By default library will use the directory of the file where config()
was called as the start directory to look for configuration files. Consider the following file structure:
project/
settings.ini
app/
settings.py
If you call config()
from project/app/settings.py
the library will start looking for configuration files at project/app
until it finds .env|*.ini|*.cfg
files.
This behavior is described more deeply on the :pyRecursiveSearch<prettyconf.loaders.RecursiveSearch>
loader. Loaders<loaders>
will help you customize how configuration discovery works. Find out more at discovery-customization
.