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.
Metadata
Release files for nyeok-dotenv 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| nyeok_dotenv-0.1.0.tar.gz | 5.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nyeok_dotenv-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / nyeok_dotenv-0.1.0.tar.gz
| Download URL | nyeok_dotenv-0.1.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05561442e39e0a96dea98c371a3bd1f66640725c93ac1d003a649d1af513f0fc
|
|
BLAKE2b-256 checksum How to use checksums |
4940f36b7d2b962338db6af21436dc854209afbd35145e8f5110beb5846cb01e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/5.15.153.1-microsoft-standard-WSL2
|
Release files / nyeok_dotenv-0.1.0-py3-none-any.whl
| Download URL | nyeok_dotenv-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c67ad9eb8f2dc07a1cc19fdfde162c9504ddf4d54c04e632196fe82cea0496c
|
|
BLAKE2b-256 checksum How to use checksums |
589b6c3d2c524eedefd7a1cd3cbc6f4a630969b808f1ee0466e5718e0616161b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/5.15.153.1-microsoft-standard-WSL2
|
Security for nyeok-dotenv 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.