Package: dotenv-settings-handler
Python-DotEnv-Settings-HandlerA Settings Handler usingpython-dotenvand/or system environment variables, to read all the settings from a Settings class based onpydantic.Instead of loading the environment variables (from now on 'env vars') asos.getenv("MY_VAR"), create a class with all your env variables, and load them assettings.MY_VAR.The Class init will look for env variables used as settings for your project. These variables can be system env variables, or be specified on a .env file.RequirementsPython 3.x (tested on Python 3.7)Libraries: pydantic (python-dotenv is not called from this library)InstallingFrom pip:pipinstall-Udotenv-settings-handlerFrom Github:gitclonehttps://github.com/David-Lor/Python-DotEnv-Settings-HandlercdPython-DotEnv-Settings-Handler
pythonsetup.pyinstallTest:pipinstall-rrequirements.txt
pythonsetup.pytestChangelog0.0.3 - Add Tests0.0.2 - Removed calling to load_dotenv from the BaseSettingsHandler init, since it would look for the .env file in the wrong path. Manually calling load_dotenv from the target project context is now required.0.0.1 - Initial releaseExamplesStart by creating a.envfile, as you would usually do using python-dotenv, or define all or some of these variables as system environment variables.DATABASE_PORT=33306
DATABASE_USER=foo
DATABASE_PASSWORD=bar
DATABASE_DB=mydbThen, create a new class inheriting from BaseSettingsHandler:BaseSettingsHandler inherits from pydantic.BaseSettings, working in a similar manner as pydantic.BaseModel. Checkpydantic documentationto know more about it. You should be able to extend your class with pydantic features (read more in the examples).Basically you must define on your custom class the wanted ENV variables as fields (class attributes), which must have the same name as on the .env file.You can define a default value to be used if a certain variable is not defined on the .env file (in the example: DATABASE_SERVER, DATABASE_PORT).You should set the data type (str, int) on the values without default value (in the example: DATABASE_USER, DATABASE_PASSWORD, DATABASE_DB). Values with a default value will use the type of that value.If an env variable without default value not exists, when creating an instance of MySettings() a pydantic exception will be raised, asking to fill a required class attribute.Calling load_dotenv() from your Python module is required if using .env file, in order to lookup for the .env file in your module directory context.# settings.pyfromdotenv_settings_handlerimportBaseSettingsHandlerfromdotenvimportload_dotenvload_dotenv()classMySettings(BaseSettingsHandler):DATABASE_SERVER="127.0.0.1"DATABASE_PORT=3306DATABASE_USER:strDATABASE_PASSWORD:strDATABASE_DB:strmy_settings=MySettings()Finally, you can import themy_settingsclass instance anywhere you want to use these settings:# connection.pyfrom.settingsimportmy_settingsassettingsimportpymysqlconnection=pymysql.connect(host=settings.DATABASE_SERVER,port=settings.DATABASE_PORT,user=settings.DATABASE_USER,password=settings.DATABASE_PASSWORD,db=settings.DATABASE_DB,charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)This is what happened to each ENV var:DATABASE_SERVER: not defined on .env, so defaults to "127.0.0.1"DATABASE_PORT: defined on .env as 33306. Since in the class defaults to 3306 (an int), is automatically casted to int by pydantic.DATABASE_USER, DATABASE_PASSWORD, DATABASE_DB: defined on .env, and have no default values, so they are required. Not defining them on the .env file, nor as system env variables, would raise a pydantic exception when creating the MySettings() instance at settings.py.Ignore env caseSince BaseSettingsHandler inherits frompydantic.BaseSettings, we can take advantage of the features of BaseSettings, such as ignoring the env case.
This means we can have the env var names in our class as lowercase, and define them in our system as uppercase, lowercase or mixed lower/uppercase. They will still be detected by pydantic and the BaseSettingsHandler.Just modify your custom Settings class with the modifiers featured by pydantic.BaseSettings (under a nestedclass Config):# settings.pyfromdotenv_settings_handlerimportBaseSettingsHandlerclassMySettings(BaseSettingsHandler):my_foo:strMY_BAR:intclassConfig:case_insensitive=Truesettings=MySettings()Now you can set your .env and/or system env variables with any case you want; i.e.my_foo,MY_foo,MY_FOO,my_FOO,my_Fooare all valid and will be accesible throughsettings.my_foo.Set env prefixAnother cool feature from pydantic.BaseSettings is setting a prefix on your env variables. By default pydantic set this asAPP_, but on BaseSettingsHandler this feature is disabled. However, you can enable it again setting theenv_prefixoption underclass Config.# settings.pyfromdotenv_settings_handlerimportBaseSettingsHandlerclassMySettings(BaseSettingsHandler):foo:strbar:intclassConfig:env_prefix="PYTHON_APP_"settings=MySettings()Now the fieldfoowill be read from the env varPYTHON_APP_FOO(note that it will look for an uppercase env var), andbarfromPYTHON_APP_BAR.