DotEnv Class
The DotEnv class is the main interface for managing environment variables.
Class Reference
- class envdot.DotEnv(filepath=None, auto_load=True)
Main class for environment variable management.
- Parameters:
Example:
from envdot import DotEnv # Auto-load from .env env = DotEnv('.env') # Create without loading env = DotEnv('.env', auto_load=False)
Methods
load()
- DotEnv.load(filepath=None, override=True, apply_to_os=True)
Load environment variables from a file.
- Parameters:
- Returns:
Self for method chaining
- Return type:
- Raises:
FileNotFoundError – If the specified file doesn’t exist
ParseError – If the file cannot be parsed
Example:
env = DotEnv('.env', auto_load=False) # Basic load env.load() # Load from different file env.load('config.json') # Load without overriding existing values env.load(override=False) # Load without affecting os.environ env.load(apply_to_os=False)
get()
- DotEnv.get(key, default=None, cast_type=None)
Get an environment variable with automatic type detection.
- Parameters:
- Returns:
The value with detected or cast type
- Return type:
Any
- Raises:
TypeConversionError – If cast_type is specified and conversion fails
Example:
env = DotEnv('.env') # Get with auto type detection debug = env.get('DEBUG') # Returns bool port = env.get('PORT') # Returns int # Get with default timeout = env.get('TIMEOUT', default=30) # Get with explicit type casting version = env.get('PORT', cast_type=str)
set()
- DotEnv.set(key, value, apply_to_os=True)
Set an environment variable.
- Parameters:
- Returns:
Self for method chaining
- Return type:
Example:
env = DotEnv('.env') # Set various types env.set('DEBUG', True) env.set('PORT', 8080) env.set('TIMEOUT', 30.5) env.set('APP_NAME', 'MyApp') # Set without affecting os.environ env.set('INTERNAL', 'value', apply_to_os=False)
save()
- DotEnv.save(filepath=None, format=None)
Save environment variables to a file.
- Parameters:
- Returns:
Self for method chaining
- Return type:
Example:
env = DotEnv('.env') env.set('NEW_KEY', 'value') # Save to original file env.save() # Save to new file env.save('backup.env') # Convert to different format env.save('config.json')
delete()
- DotEnv.delete(key, remove_from_os=True)
Delete an environment variable.
- Parameters:
- Returns:
Self for method chaining
- Return type:
Example:
env = DotEnv('.env') # Delete from envdot and os.environ env.delete('OLD_KEY') # Delete from envdot only env.delete('TEMP_KEY', remove_from_os=False)
all()
keys()
clear()
- DotEnv.clear(clear_os=False)
Clear all stored variables.
- Parameters:
clear_os (bool) – Whether to also clear from os.environ (default: False)
- Returns:
Self for method chaining
- Return type:
Example:
env = DotEnv('.env') # Clear internal storage only env.clear() # Clear both internal storage and os.environ env.clear(clear_os=True)
show()
Magic Methods
__getitem__
- DotEnv.__getitem__(key)
Dictionary-style access for getting values.
Example:
env = DotEnv('.env') value = env['DATABASE_URL']
__setitem__
- DotEnv.__setitem__(key, value)
Dictionary-style access for setting values.
Example:
env = DotEnv('.env') env['NEW_KEY'] = 'value'
__contains__
- DotEnv.__contains__(key)
Check if a key exists using
inoperator.Example:
env = DotEnv('.env') if 'API_KEY' in env: print("API key is configured")
__getattr__
- DotEnv.__getattr__(key)
Attribute-style access for getting values.
Example:
config = DotEnv('.env') debug = config.DEBUG port = config.PORT
__setattr__
- DotEnv.__setattr__(key, value)
Attribute-style access for setting values.
Example:
config = DotEnv('.env') config.DEBUG = True config.PORT = 9000
__repr__
- DotEnv.__repr__()
String representation of the DotEnv instance.
Example:
env = DotEnv('.env') print(env) # DotEnv(filepath=.env, vars=18)