Skip to main content

Settings handler to load settings from a DotEnv file or system env variables, using python-dotenv and pydantic.

Project description

Python-DotEnv-Settings-Handler

A Settings Handler to be used with python-dotenv to load and read all the settings from a Settings class.

Instead of loading the env variables as os.getenv("MY_VAR"), create a class with all your env variables, and load them as settings.MY_VAR.

Requirements

  • Python 3.x (tested on Python 3.7)
  • Libraries: python-dotenv, pydantic

Changelog

  • 0.0.1 - Initial release

Example

Start by creating a .env file, 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=mydb

Then, create a new class inheriting from SettingsHandler:

  • SettingsHandler inherits from pydantic.BaseSettings, working in a similar manner as pydantic.BaseModel. Check pydantic documentation to know more about it.
  • Basically you must define on your custom class the wanted ENV variables, 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).
  • 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.
# settings.py

from dotenv_settings_handler import SettingsHandler

class MySettings(SettingsHandler):
    DATABASE_SERVER = "127.0.0.1"
    DATABASE_PORT = 3306
    DATABASE_USER: str
    DATABASE_PASSWORD: str
    DATABASE_DB: str

my_settings = MySettings()

Finally, you can import the my_settings class instance anywhere you want to use these settings:

# connection.py

from .settings import my_settings as settings
import pymysql

connection = 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dotenv-settings-handler-0.0.1.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file dotenv-settings-handler-0.0.1.tar.gz.

File metadata

  • Download URL: dotenv-settings-handler-0.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for dotenv-settings-handler-0.0.1.tar.gz
Algorithm Hash digest
SHA256 cd53ec70ea26616f928b1cebbc4fe1d26647da5265cb7122ee5c09ad061608fb
MD5 7cb49948ff974817b5abdcf236830598
BLAKE2b-256 a755fc5982b255f7b4b2e89e22d0ee732f96a5478d1958b82f5065c27b8c1d0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page