Package: python-env-loader
python-env-loaderReads the variables from.envfiles in similar way that is used inReact.jsand adds them to environment variables, automatically guessing and parsing to correct types in
Python. It is great for managing app settings during development, tests and in production
without writing additional configuration, and to hold settings separated by
environment type clearly in a one place.UsageCallload_envon the application start, and it will load environment variables from a
certain set of.envfiles in the selected directory and return andEnvFileobject.
After that, you can get values by usinggetmethod ofEnvFileobject, or by reading it
directly from the environment variables (by usingos.environ).Example.envfile looks like this:SECRET_KEY=test-secret-key.123EXTERNAL_API_URL=api.example.comEXTERNAL_API_KEY=this-is-external-api-keyDEBUG=trueALLOWED_HOSTS=127.0.0.1:8000,0.0.0.0:8000,localhost:8000Getting startedInstallationInstall the latest version with:pipinstall-Upython-env-loaderFirst stepsAssuming you have created the.envand.env.localfiles inside your project root directory:.
├── .env
└── .env.localAnd they have the following content:# .envDEBUG=false# maps to False in pythonALLOWED_HOSTS=,# maps to empty list []# .env.localDEBUG=true# maps to True in pythonALLOWED_HOSTS=127.0.0.1:8000,0.0.0.0:8000,localhost:8000# maps to list [127.0.0.1:8000, 0.0.0.0:8000, localhost:8000]Add the following code to your settings module - for examplesettings.pyin Django:# settings.pyfromenv_loaderimportload_envenv=load_env()# if env files are in your root directory of the projector:# settings.pyfromenv_loaderimportload_envenv=load_env("/app")# if your env file is stored in /app directoryParsed key/value pairs from the.envand.env.localfiles are now accessible inenvobject:# settings.pyenv.get("DEBUG")# returns True, because .env.local file has higher priorityenv.get("IS_TEST_ENV",False)# returns False, because this key is not present in any env fileor you can use them as system environment variable and they can be conveniently accessed
viaos.getenv(), but then every value is received as a string:# settings.pyimportosDEBUG=os.getenv("DEBUG")# returns "true" AS A STRINGAutomatic parsingpython-env-loaderautomatically guesses the Python type of the environment variable. If a variable consists
only of digits, it assumes that it's an integer. If a value consists only of a digits and has a one dot, then
it's treated as a float.More custom parsing:Python value.env valueTruetrue, True, y, yesFalsefalse, False, n, noNonenull, NoneEmpty string is mapped toNone.You can disable automatic parsing by passingauto_parse=Falsetoload_envfunction:fromenv_loaderimportload_env,EnvTypesenv=load_env(auto_parse=False)Switching between setupsWhen you're loading your env settings, you can switch between 4 of a different setups of them:default,development,productionandtest. By default,defaultis used, but you can
select different setup by passing a value toenv_typeargument ofload_envfunction:fromenv_loaderimportload_env,EnvTypesenv=load_env(env_type=EnvTypes.DEVELOPMENT_ENV)Files hierarchyFor each setup, files on the left have more priority than files on the right:default:.env.local,.envdevelopment:.env.development.local,.env.development,.env.local,.envproduction:.env.production.local,.env.production,.env.local,.envtest:.env.test.local,.env.test,.env(note.env.localis missing)