A simple Python package to load, validate, and type-cast environment variables from .env files using a schema.
Project description
dotenv-schema
A simple, lightweight Python tool to load, validate, and type-cast environment variables from .env files using a schema. This package helps you manage configuration in a type-safe way while keeping your secrets out of your codebase.
Features
- 📝 Load variables from
.envfiles into your Python environment - ✅ Validate required environment variables
- 🔄 Automatic type casting (string, int, float, boolean)
- 🛡️ Schema validation with default values
- 🪶 Lightweight with zero dependencies
Installation
pip install dotenv-schema
For development:
git clone https://github.com/skyspec28/dotenv_schema.git
cd dotenv_schema
pip install .
Usage
Basic Usage
from dotenv_schema import load_env_file
# Load variables from .env file
env_vars = load_env_file()
print(env_vars) # {'DATABASE_URL': 'postgres://...', 'DEBUG': 'true'}
# Access through os.environ
import os
print(os.environ['DATABASE_URL']) # 'postgres://...'
With Schema Validation
from dotenv_schema import load_env_file, apply_schema
# Define your schema
schema = {
'DATABASE_URL': {
'required': True,
'type': str
},
'DEBUG': {
'type': bool,
'default': False
},
'PORT': {
'type': int,
'default': 8000
}
}
# Load and validate
raw_env = load_env_file()
config = apply_schema(raw_env, schema)
print(config['DEBUG']) # True (boolean, not string)
print(config['PORT']) # 8000 (integer, not string)
Example .env file
# Database configuration
DATABASE_URL=postgres://user:password@localhost:5432/mydb
# Application settings
DEBUG=true
LOG_LEVEL=info
# API keys (keep these secret!)
API_KEY=your-secret-key-here
API Reference
load_env_file(path='.env') -> dict
Loads environment variables from a file into os.environ and returns them as a dictionary.
- path: Path to the .env file (default: '.env')
- returns: Dictionary of environment variables
apply_schema(raw_env: dict, schema: dict) -> dict
Validates and transforms environment variables according to the provided schema.
- raw_env: Dictionary of raw environment variables
- schema: Dictionary defining the validation rules
- returns: Dictionary of validated and transformed environment variables
License
MIT License - see the LICENSE file for details.
Testing
The project uses pytest for testing. To run the tests:
# Install the package with development dependencies
pip install ".[dev]"
# Run tests
pytest
# Run tests with coverage report
pytest --cov=dotenv_schema
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please make sure to update tests as appropriate.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dotenv_schema-0.1.1.tar.gz.
File metadata
- Download URL: dotenv_schema-0.1.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f1ba1c34dcdd04762af6d0739b79c4b04349a26ed3f5b6f44bbe58453235633
|
|
| MD5 |
357102878878933074d34b78d1797f6f
|
|
| BLAKE2b-256 |
19d7722d6cfe6a290062266a386eded66d3ea0a4e48f62819ac00143509a101a
|
Provenance
The following attestation bundles were made for dotenv_schema-0.1.1.tar.gz:
Publisher:
publish-to-pypi.yml on skyspec28/dotenv_schema
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dotenv_schema-0.1.1.tar.gz -
Subject digest:
8f1ba1c34dcdd04762af6d0739b79c4b04349a26ed3f5b6f44bbe58453235633 - Sigstore transparency entry: 192918059
- Sigstore integration time:
-
Permalink:
skyspec28/dotenv_schema@ccf612a9c78c4461684f766a0c6a51367a6358c4 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/skyspec28
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@ccf612a9c78c4461684f766a0c6a51367a6358c4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dotenv_schema-0.1.1-py3-none-any.whl.
File metadata
- Download URL: dotenv_schema-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6db3fc7c30585c294bd21e4f8262f42ceacdfe17ac55970117c035213c689912
|
|
| MD5 |
f3e3bd734b767646ee1e0afdcfb89b5d
|
|
| BLAKE2b-256 |
90db280313a97c9fa34cb40ad8be6071bde31dc99a483c02386fa2b3188109a1
|
Provenance
The following attestation bundles were made for dotenv_schema-0.1.1-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on skyspec28/dotenv_schema
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dotenv_schema-0.1.1-py3-none-any.whl -
Subject digest:
6db3fc7c30585c294bd21e4f8262f42ceacdfe17ac55970117c035213c689912 - Sigstore transparency entry: 192918060
- Sigstore integration time:
-
Permalink:
skyspec28/dotenv_schema@ccf612a9c78c4461684f766a0c6a51367a6358c4 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/skyspec28
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@ccf612a9c78c4461684f766a0c6a51367a6358c4 -
Trigger Event:
release
-
Statement type: