Skip to main content

A comprehensive CLI tool to manage environment variables in .env files with complete syntax support

Project description

dotenv-tools

A comprehensive CLI tool to load, unload, and manage environment variables in .env files with complete syntax support.

Python 3.8+ License: MIT

Features

All Assignment Operators: =, :=, +=, ?=
Variable Expansion: ${VAR}, ${VAR:-default}, ${VAR:=default}, ${VAR:+alt}
Shell Export Support: export KEY=value
Smart Parsing: Comments, quotes, escapes, multiline values
Load/Unload: Load variables and easily remove them later
Set/Edit/Remove: Manage .env files directly from command line
Flexible: Load from any path or auto-discover .env files
Cross-Platform: Works on Windows, macOS, and Linux \

Quick Start

Installation

Install from PyPI:

pip install dotenv-tools

Or install from source:

git clone https://github.com/LousyBook94/load-dotenv.git
cd load-dotenv
pip install -e .

Basic Usage

Load from .env in current directory:

load-dotenv

Load from a specific file:

load-dotenv /path/to/my-custom.env

Unload all loaded variables:

unload-dotenv

Set a variable in .env:

set-dotenv PORT 3000
set-dotenv API_KEY=secret123

Remove a variable:

set-dotenv --remove API_KEY

List all variables:

set-dotenv --list

Edit .env file:

set-dotenv --edit

Verbose output:

load-dotenv --verbose
set-dotenv --verbose --list

Assignment Operators

This tool supports all major assignment operators:

Operator Description Example
= Standard assignment KEY=value
:= Immediate expansion PATH:=$PATH:/new/path
+= Append to existing PATH+=/additional/path
?= Conditional assignment API_KEY?=${DEFAULT_KEY}

Variable Expansion

Full support for variable expansion syntax:

Syntax Description Example
${VAR} Expand from environment GREETING=${USER}
${VAR:-default} Use default if unset PORT=${PORT:-8080}
${VAR:=default} Assign default if unset DB_NAME:=mydb
${VAR:+alt} Use alternate if set DEBUG=${DEBUG:+1}

Examples

Example .env file

# Database configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydatabase
DB_USER=admin
DB_PASS=secret123

# Application settings
APP_NAME=MyApp
APP_ENV=development
DEBUG=true

# Variable expansion
APP_URL=http://${DB_HOST}:${PORT}

# Conditional assignment (only set if not already set)
API_KEY?=${DEFAULT_API_KEY}

# Append to existing
PATH+=/opt/myapp/bin

# Immediate expansion
FULL_PATH:=${HOME}/myapp/data

Using with Commands

Set variables directly:

# Set individual variables
set-dotenv PORT 3000
set-dotenv API_KEY=secret123

# List all variables in .env
set-dotenv --list

# Remove a variable
set-dotenv --remove OLD_KEY

# Edit .env file
set-dotenv --edit

Load and unload environment:

# Load environment
load-dotenv

# Check if variables are loaded
echo $DB_HOST  # outputs: localhost

# Unload when done
unload-dotenv

Use Cases

Development Workflow

Set up your environment:

# Add variables to .env
set-dotenv PORT 3000
set-dotenv DATABASE_URL=postgres://localhost/mydb

# Load and use
load-dotenv
python app.py

# Clean up
unload-dotenv

Managing multiple environments:

# Development
set-dotenv --file .env.development DEBUG=true
load-dotenv .env.development

# Production
set-dotenv --file .env.production DEBUG=false
load-dotenv .env.production

CI/CD Pipelines

# Load environment for testing
load-dotenv --verbose

# Run tests
pytest tests/

# Unload after tests
unload-dotenv --force

Docker/Shells

Load environment in your shell:

# Add to ~/.bashrc or ~/.zshrc
load-dotenv() {
    python -m dotenv_tools.cli load-dotenv "$@"
}

unload-dotenv() {
    python -m dotenv_tools.cli unload-dotenv "$@"
}

Documentation

For complete documentation, see USAGE.md which includes:

  • Detailed syntax reference
  • All command-line options
  • Advanced examples
  • Troubleshooting guide

Requirements

  • Python 3.8 or higher
  • click package (automatically installed)

Installation from Source

git clone https://github.com/LousyBook94/load-dotenv.git
cd load-dotenv

# Install in development mode
pip install -e .

# Or build and install
pip install .

Running Tests

# Install test dependencies
pip install -e .[dev]

# Run tests
pytest

# Run with coverage
pytest --cov=src/load_dotenv

Building for Distribution

# Build source and wheel distributions
python -m build

# Upload to PyPI (requires twine)
python -m twine upload dist/*

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

LousyBook01 - @LousyBook94

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Roadmap

  • Auto-completion for shell integration
  • .env template generation
  • Environment diffing
  • YAML/JSON export support

Issues

If you encounter any issues or have questions, please file an issue on GitHub.

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_tools-0.0.1.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dotenv_tools-0.0.1-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file dotenv_tools-0.0.1.tar.gz.

File metadata

  • Download URL: dotenv_tools-0.0.1.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for dotenv_tools-0.0.1.tar.gz
Algorithm Hash digest
SHA256 552ca4d10779df0ba3444a4e3e572a4b679ac3c3d971a91702fe3a2a58c07ab1
MD5 79ad7236530809e23524b32043e4c841
BLAKE2b-256 a729e37672d202fc99d91ed070e700f4849687ee2bb3c2fbea5868871aa78123

See more details on using hashes here.

File details

Details for the file dotenv_tools-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dotenv_tools-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for dotenv_tools-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8a7391cd29c5b98442fe7f3d3083db726a4631f145fb25f7ab29578e2dcba6a
MD5 c07f025ed852debdce8c28d0b2351c7a
BLAKE2b-256 1ca516281e4787e4a58dc9772595b578bddfcaa998c409ab8c0c18d4ab31f74f

See more details on using hashes here.

Supported by

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