Skip to main content

A Python library for loading environment variables from shell scripts with type-safe reading capabilities

Project description

Envsh

A Python library for loading environment variables from shell scripts with variable interpolation and type-safe reading capabilities.

Key Advantage: Variable Interpolation

Unlike traditional .env files, envsh uses shell scripts which support full variable interpolation, calculations, and dynamic configurations:

# ❌ This WON'T work in .env files:
DATABASE_URL=postgresql://$HOST:5432/$DB_NAME
WORKER_PORTS=$BASE_PORT,$(($BASE_PORT + 1)),$(($BASE_PORT + 2))

# ✅ This WORKS with envsh (.sh files):
export HOST="localhost" 
export DB_NAME="myapp"
export BASE_PORT=8000

export DATABASE_URL="postgresql://$HOST:5432/$DB_NAME"
export WORKER_PORTS="$BASE_PORT,$(($BASE_PORT + 1)),$(($BASE_PORT + 2))"
export CONFIG_ARRAY="value1,$HOST,value3,port-$BASE_PORT"

Features

  • Variable Interpolation: Use $VAR and ${VAR} syntax like in shell scripts
  • Shell Calculations: Support for $((...)) arithmetic and $(command) execution
  • Type-safe Reading: Convert to int, float, str, list[int], list[str] with validation
  • Smart Array Parsing: Comma-separated values with automatic trimming
  • Multi-directory Search: Load from multiple directories automatically
  • Error Handling: Clear error messages for debugging

Installation

pip install envsh

Quick Start: Variable Interpolation

  1. Create a shell script with interpolated variables:
# config.sh
export HOST="localhost"
export BASE_PORT=8000
export DB_NAME="myapp"

# Variable interpolation - the main advantage!
export DATABASE_URL="postgresql://$HOST:5432/$DB_NAME"
export API_ENDPOINT="http://$HOST:$BASE_PORT/api"
export WORKER_PORTS="$BASE_PORT,$(($BASE_PORT + 1)),$(($BASE_PORT + 2))"
export SERVICE_URLS="http://$HOST:$BASE_PORT,https://$HOST:8443"
  1. Load and use with type safety:
import envsh

# Load environment variables with interpolation
envsh.load()

# Read interpolated values
database_url = envsh.read_env('DATABASE_URL', str)
# Result: "postgresql://localhost:5432/myapp"

worker_ports = envsh.read_env('WORKER_PORTS', list[int])  
# Result: [8000, 8001, 8002] - calculated from BASE_PORT!

service_urls = envsh.read_env('SERVICE_URLS', list[str])
# Result: ["http://localhost:8000", "https://localhost:8443"]

Comparison with .env Files

Feature .env files envsh (shell scripts)
Variable interpolation ❌ No ✅ Full support
Calculations ❌ No $(($VAR + 1))
Command execution ❌ No $(date), $(nproc)
Dynamic arrays ❌ No "$VAR1,$VAR2,suffix"
Type safety ❌ Strings only ✅ int, float, str, list[int], list[str]
Array parsing ❌ Manual ✅ Automatic comma-split

API Reference

load(search_paths=None, verbose=False)

Loads environment variables from .sh files in the specified directories.

Parameters:

  • search_paths (list[str], optional): Directories to search for .sh files. Defaults to ['.', '..']
  • verbose (bool, optional): Print information about loaded files. Defaults to False

Example:

# Load from current and parent directory
envsh.load()

# Load from specific directories
envsh.load(['./config', './env'], verbose=True)

read_env(name, return_type)

Reads an environment variable with the specified type.

Parameters:

  • name (str): Name of the environment variable
  • return_type (Type): Expected return type (int, float, str, list[int], or list[str])

Returns:

  • The environment variable value converted to the specified type

Raises:

  • EnvironmentError: If the environment variable is not set
  • ValueError: If the value cannot be converted to the specified type
  • TypeError: If the return type is not supported

Examples:

# Read as integer
port = envsh.read_env('PORT', int)

# Read as string
host = envsh.read_env('HOST', str)

# Read as integer array (comma-separated)
ports = envsh.read_env('PORTS', list[int])  # "8000,8001,8002" -> [8000, 8001, 8002]

# Read as string array (comma-separated)
hosts = envsh.read_env('HOSTS', list[str])  # "localhost,example.com" -> ["localhost", "example.com"]

Variable Interpolation Examples

Basic Interpolation

export HOST="localhost"
export PORT=8000
export API_URL="http://$HOST:$PORT/api"  # → "http://localhost:8000/api"

Arrays with Interpolation

export BASE_PORT=8000
export SERVICES="web:$BASE_PORT,api:$(($BASE_PORT + 1)),ws:$(($BASE_PORT + 2))"
# → ["web:8000", "api:8001", "ws:8002"]

Dynamic Configuration

export ENV="production"
export LOG_LEVEL="INFO"
export CONFIG_NAME="app-$ENV-$(date +%Y%m%d)"  # → "app-production-20250811"
export WORKER_COUNT=$(nproc)  # → Number of CPU cores

Error Handling

The library provides clear error messages for common issues:

# Missing environment variable
try:
    value = envsh.read_env('MISSING_VAR', str)
except EnvironmentError as e:
    print(f"Variable not found: {e}")

# Invalid integer conversion
os.environ['INVALID_NUMBER'] = 'not_a_number'
try:
    number = envsh.read_env('INVALID_NUMBER', int)
except ValueError as e:
    print(f"Conversion error: {e}")

# Unsupported type
try:
    value = envsh.read_env('SOME_VAR', dict)
except TypeError as e:
    print(f"Type error: {e}")

Use Cases

  • Configuration Management: Load application configuration from shell scripts
  • Development Environments: Share environment setup across team members
  • CI/CD Pipelines: Load environment-specific variables from shell scripts
  • Docker Deployments: Initialize container environments from shell scripts

Requirements

  • Python 3.11+
  • Unix-like system with Bash (Linux, macOS)
  • Windows: Git Bash or MSYS2

Contributing

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

License

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

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

envsh-0.1.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

envsh-0.1.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file envsh-0.1.2.tar.gz.

File metadata

  • Download URL: envsh-0.1.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.8.0-79-generic

File hashes

Hashes for envsh-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ec26816e40d4e188221cae1ae4bd8e017a3f2cdb7084e03f720c4fbae2a5460f
MD5 574d145fdf4b90554960069581d08b7f
BLAKE2b-256 4b903f34f878517d1ec04fd009e9f098a3603de3136e2a9314e1b25bfe3b7b6f

See more details on using hashes here.

File details

Details for the file envsh-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: envsh-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.8.0-79-generic

File hashes

Hashes for envsh-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 63dd7b548aa8eb26862190149a3cd93ff7763bfbc57b3888ae91aca8589d84b4
MD5 4e40a7148710d09d76e5cfca88eb5927
BLAKE2b-256 a487369f8b684b7cb6a06fc42416df2bf4fb8804ac3941d71697b223f931f686

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