Skip to main content

No project description provided

Project description

nyeok-dotenv

nyeok-dotenv manages environment variables from .env files for different build types.

Installation

To install nyeok-dotenv, run:

pip install nyeok-dotenv

Usage

Setup Directory Structure

sample_env.py will load .env files from the same directory.

.
├── .env.dev
├── .env.prod
├── .env.stage
├── .secret
├── main.py
└── sample_env.py

Create a New Environment Variable Class

# sample_env.py

from enum import StrEnum, unique
from nyeok_dotenv.EnvBase import EnvBase

@unique
class RuntimeType(StrEnum):
    # Enum name will be used in code
    # Enum value will be used in RUNTIME_TYPE environment variable
    PROD = "prod"
    STAGE = "staging"
    DEV = "dev"

class SampleEnv(EnvBase[RuntimeType]):
    # Define environment variables as type hints

    # nyeok-dotenv will not load _ prefix variables
    _hidden_variable: str

    # From .env
    POSTGRES_USER: str

    # From .secret
    POSTGRES_PASSWORD: str

# Create the environment class and export it
env = SampleEnv(
    runtime_type_cls=RuntimeType,
    files_to_load={
        RuntimeType.PROD: [".env.prod"],  # secret will be set by k8s
        RuntimeType.STAGE: [".env.stage", ".secret"],
        RuntimeType.DEV: [".env.dev", ".secret"],
    },
    # .env files will be searched in the same directory as sample_env.py
    directory=os.path.dirname(__file__),

    # Environment variable with key "RUNTIME_TYPE" will be used to determine the runtime type
    env_variable_for_type="RUNTIME_TYPE",

    # Default to RuntimeType.DEV if RUNTIME_TYPE is not set
    default_type=RuntimeType.DEV,
)

Load Environment Variables

Run the application with the desired runtime type:

RUNTIME_TYPE=dev; python -m main
# main.py

from .sample_env import env

# Check the runtime type
assert env._runtime_type == RuntimeType.DEV

print(env.POSTGRES_USER)
print(env.POSTGRES_PASSWORD)

Examples

Handling Secrets

You may have .secret files locally but don't want to include them in production. Instead, set environment variables directly in Kubernetes, and nyeok-dotenv will load them.

.
├── .env.prod
├── .env.dev
├── .secret (not present in production)
├── use_secret.py
└── k8s
    ├── .secret (generated from .secret)
    ├── kustomization.yaml
    └── deployment.yaml

Create Kubernetes Secret from Local .secret File (Using Kustomize)

# ./k8s/kustomization.yaml
secretGenerator:
  - name: backend-secret
    envs:
      - ./.secret
# Update the secret file to match the local development environment
cp .secret k8s/.secret
kubectl apply -k k8s

Load Secret in Kubernetes Deployment

containers:
  - name: backend-container
    env:
      - name: POSTGRES_PASSWORD
        valueFrom:
          secretKeyRef:
            name: backend-secret
            key: POSTGRES_PASSWORD

Use Env in Code

You can now use env.POSTGRES_PASSWORD in both development and production environments.

Without RuntimeType

[!CAUTION] This feature is not implemented yet.

If you want to manage environment variables for only one build type, you can use nyeok-dotenv without RuntimeType.

# no_runtime_type.py

from nyeok_dotenv.EnvBase import EnvBase

class SampleEnv(EnvBase[RuntimeType]):
    # From .env
    POSTGRES_USER: str
    # From .secret
    POSTGRES_PASSWORD: str

env = SampleEnv(
    files_to_load=[".env", ".secret"],
    directory=os.path.dirname(__file__),
)

Load Files from Different Directories

Specify the directory to load .env files from different locations.

.
├── dir1
│   ├── .env
│   └── .secret
└── from_different_directory.py
# from_different_directory.py

from nyeok_dotenv.EnvBase import EnvBase

class SampleEnv(EnvBase[RuntimeType]):
    # From .env
    POSTGRES_USER: str
    # From .secret
    POSTGRES_PASSWORD: str

env = SampleEnv(
    files_to_load=[".env", ".secret"],
    directory=os.path.join(os.path.dirname(__file__), "dir1"),
)

[!NOTE]
If you don't specify directory, nyeok-dotenv will search .env files from the current working directory.


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

nyeok_dotenv-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

nyeok_dotenv-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file nyeok_dotenv-0.1.0.tar.gz.

File metadata

  • Download URL: nyeok_dotenv-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for nyeok_dotenv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 05561442e39e0a96dea98c371a3bd1f66640725c93ac1d003a649d1af513f0fc
MD5 d5e0c685e607f661bce69dd4dfc9791f
BLAKE2b-256 4940f36b7d2b962338db6af21436dc854209afbd35145e8f5110beb5846cb01e

See more details on using hashes here.

File details

Details for the file nyeok_dotenv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nyeok_dotenv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for nyeok_dotenv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c67ad9eb8f2dc07a1cc19fdfde162c9504ddf4d54c04e632196fe82cea0496c
MD5 39de165959f529a203f550e5a9bb4b11
BLAKE2b-256 589b6c3d2c524eedefd7a1cd3cbc6f4a630969b808f1ee0466e5718e0616161b

See more details on using hashes here.

Supported by

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