Skip to main content

EnvClass

python_versions gnu version testing

Manage environment variables in a simple and elegant way

Made from 100% pure Python

Why does this exist

To manage environment variables in an easy way that works for any device (like a cell phone with termux)

If you intend to run in production, use pydantic-settings

Installation

pip install envclass

Quick Start

.env

HOST=0.0.0.0
PORT=1234

CONFIG_FILE=
from envclass import EnvClass


# When declaring the class with _env_file it
# reads the file and casts all attributes
class MyEnv(EnvClass):
    _env_file = '.env'

    TESTING: bool = False

    HOST: str = 'localhost'
    PORT: int

    CONFIG_FILE: str


# Return: '0.0.0.0'
MyEnv.HOST

# Return: False
MyEnv.TESTING

# Return: 1234
MyEnv.PORT

# Return: None
MyEnv.CONFIG_FILE

Supported Types

Currently, only primitive types have been tested, such as:

  • str
  • int
  • bool
  • float

Booleans

Attributes follow Python language conventions for conversion, but bool attributes have specific interpretations when reading environment variables:

bool attributes can be:

  • True, true, or 1 for true.
  • False, false, or 0 for false.

Read Only Attributes

When this configuration is defined, it is not possible to change the attributes

Example:

from envclass import EnvClass


class EnvLock(EnvClass)
    KEY: str = None

# Generates an AttributeError stating 
# that it is read-only
EnvLock.KEY = 'Value'

Lower Attrubutes

Lowercase attributes work the same way, I just thought leaving everything capitalized would look better

Since the name is closest to the environment variable read

Example:

.env

LOWER_KEY='lower'
from envclass import EnvClass

class EnvLower(EnvClass):
    _env_file = '.env'
    lower_key = 'upper'

# Return: 'lower'
EnvLower.lower_key

See your variables

The class is seen this way:

.env

ENV_A=3
ENV_B=1
ENV_C=10
from envclass import EnvClass

class Env(EnvClass):
    _env_file = '.env'
    _prefix = 'ENV'

    A: str
    B: bool = False
    C: int

print(Env)

Output:

ENV_A='3'
ENV_B=True
ENV_C=10

Special Attributes

Env File

By default, this is set to None.

It is used to read the file like .env

Example:

# file: no_load_env.py

from envclass import EnvClass


class NoEnv(EnvClass):
    WAIT_TIME: int = 10


print(NoEnv.WAIT_TIME)

Execution on Linux:

$ WAIT_TIME=5 python no_load_env.py
5

Strict Mode

By default, this is set to True.

This allows using environ[key] to signal when an environment variable is not defined, generating the default KeyError error if the variable does not have a default value. If set to False, attributes that do not exist will return None.

Examples:

from envclass import EnvClass

# Disabled strict mode
class NotStrict(EnvClass):
    _strict = False
    NOT_EXISTS: str

# Returns None
NotStrict.NOT_EXISTS


# Enabled strict mode
# Generates a KeyError
class Strict(EnvClass):
    _strict = True
    NOT_EXISTS: str

Prefix

By default, there is None

This allows adding a string at the beginning of the environment variable name, making it easier to organize.

Example:

.env

DB_USER=dev_user
DB_KEY=dev_key_123
from envclass import EnvClass

class DataBase(EnvClass):
    _env_file = '.env'
    _prefix = 'DB'

    NAME: str = 'Dev'
    HOST: str = 'localhost'

    USER: str
    KEY: str

# Return: 'Dev'
DataBase.NAME

# Return: dev_key_123
DataBase.KEY

Metadata

Release files for envclass 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for envclass 1.1.0
File Size Uploaded
envclass-1.1.0.tar.gz 17.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for envclass 1.1.0
File Interpreter ABI Platform
envclass-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 34.5 kB

Release files / envclass-1.1.0.tar.gz

Download URL envclass-1.1.0.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bc4b66b31b5aac0cc9b760936c03809dd7e73aae0f7766ca31e03d6d8bbcc29d
BLAKE2b-256 checksum
How to use checksums
b48778e1fb1472eb51db46060fc3d75928ea54e3b00632b885f18f47e22e8e5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / envclass-1.1.0-py3-none-any.whl

Download URL envclass-1.1.0-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9750a789c1635e85b1077ca7d89c84cf5fccd1eb316921fa9a0038394bea8d4
BLAKE2b-256 checksum
How to use checksums
5e2c098625ef89beb47fd87fce524a3bafd738603cc56c9e338ae099567afc14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page