Complete guide to Python's os.environ function covering environment variable access, modification, and practical examples.
Last modified April 11, 2025
This comprehensive guide explores Python’s os.environ mapping, which provides access to environment variables. We’ll cover reading, modifying, and practical examples of environment variable usage in Python applications.
The os.environ is a dictionary-like object that represents the environment variables of the current process. It’s populated when Python starts.
Environment variables are name-value pairs stored in the operating system. They’re commonly used for configuration and passing information to processes.
Environment variables can be accessed like dictionary items. This example shows how to read common system variables and handle missing ones.
access_variables.py
import os
print(f"User’s home directory: {os.environ[‘HOME’]}") print(f"Current PATH: {os.environ[‘PATH’]}")
python_path = os.environ.get(‘PYTHONPATH’, ‘Not set’) print(f"PYTHONPATH: {python_path}")
try: print(f"Non-existent var: {os.environ[‘NON_EXISTENT’]}") except KeyError: print(“Variable NON_EXISTENT not found”)
The first part directly accesses variables that should exist. The second uses get() for safe access. The third demonstrates exception handling.
Always consider that environment variables might be missing or empty in production code.
Environment variables can be modified during runtime, but changes only affect the current process and its children. This example demonstrates modifications.
modify_variables.py
import os
print(f"Original PATH: {os.environ.get(‘PATH’, ‘’)[:50]}…")
new_path = “/usr/local/custom/bin” os.environ[‘PATH’] = f"{new_path}:{os.environ[‘PATH’]}" print(f"Modified PATH: {os.environ.get(‘PATH’, ‘’)[:50]}…")
os.environ[‘CUSTOM_VAR’] = “special_value” print(f"CUSTOM_VAR: {os.environ[‘CUSTOM_VAR’]}")
if ‘TEMP_VAR’ in os.environ: del os.environ[‘TEMP_VAR’] print(“Deleted TEMP_VAR”)
This shows how to modify existing variables, create new ones, and delete variables. Changes are only visible within the current Python process.
Modified environment variables will be inherited by any subprocesses spawned from this Python process.
You can check if an environment variable exists before accessing it. This example demonstrates several methods for checking existence.
check_existence.py
import os
if ‘HOME’ in os.environ: print(“HOME variable exists”)
lang = os.environ.get(‘LANG’, ’en_US.UTF-8’) print(f"Language: {lang}")
editor = os.getenv(‘EDITOR’, ‘vim’) print(f"Default editor: {editor}")
timezone = os.getenv(‘TZ’) if timezone is not None: print(f"Timezone: {timezone}") else: print(“Timezone not set”)
The first method uses direct dictionary-style checking. The second and third use get() and getenv() with defaults. The fourth checks for None explicitly.
Choose the method that best fits your error handling strategy and code style.
Environment variables modified in Python are passed to subprocesses. This example shows how to control the environment for child processes.
subprocess_env.py
import os import subprocess
print(f"Original ENV_VAR: {os.getenv(‘ENV_VAR’, ‘Not set’)}")
os.environ[‘ENV_VAR’] = ‘parent_value’
subprocess.call([‘python3’, ‘-c’, ‘import os; print(f"Child ENV_VAR: {os.getenv('ENV_VAR')}")’])
custom_env = os.environ.copy() custom_env[‘ENV_VAR’] = ‘custom_value’ subprocess.call([‘python3’, ‘-c’, ‘import os; print(f"Custom Child ENV_VAR: {os.getenv('ENV_VAR')}")’], env=custom_env)
The first subprocess inherits the modified environment. The second uses a custom environment dictionary. The original environment remains unchanged.
When using subprocess.Popen or similar, you can pass a complete custom environment dictionary if needed.
For binary environment variable handling, Python provides os.environb. This is useful when dealing with non-UTF-8 environment variables.
environ_binary.py
import os
os.environb[b’BINARY_VAR’] = b’special_value\xff’
print(f"Binary var: {os.environb[b’BINARY_VAR’]}")
str_var = “unicode_text” os.environb[b’BYTES_VAR’] = str_var.encode(‘utf-8’) print(f"Decoded var: {os.environb[b’BYTES_VAR’].decode(‘utf-8’)}")
if b’PATH’ in os.environb: print(f"PATH exists in binary form")
This example shows how to work with environment variables as bytes instead of strings. The environb object behaves like environ but uses bytes for keys/values.
Use environb when dealing with non-text environment variables or when you need exact byte representation.
Environment variables can contain sensitive data. This example demonstrates secure handling practices for environment variables.
security.py
import os from getpass import getpass
if ‘DB_PASSWORD’ not in os.environ: os.environ[‘DB_PASSWORD’] = getpass(“Enter database password: “)
print(“Database configuration loaded”) print(f"DB_HOST: {os.getenv(‘DB_HOST’)}”)
safe_env = { ‘DB_HOST’: os.getenv(‘DB_HOST’), ‘DB_USER’: os.getenv(‘DB_USER’) }
os.environ.pop(‘DB_PASSWORD’, None)
This shows secure practices: interactive input for secrets, avoiding logging sensitive data, creating safe environments for subprocesses, and cleaning up.
Always treat environment variables as potentially visible in process listings and logs.
Use for configuration: Store settings in environment variables
Provide defaults: Always handle missing variables gracefully
Document requirements: List needed variables in your docs
Secure sensitive data: Don’t log or expose secrets
Consider .env files: Use python-dotenv for development
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.