How can I access environment variables in Python?
Categories:
Accessing Environment Variables in Python

Learn how to effectively read, set, and manage environment variables in Python for flexible and secure application configuration.
Environment variables are a fundamental mechanism for configuring applications, especially in different deployment environments (development, testing, production). They allow you to store sensitive information like API keys, database credentials, or configuration settings outside your codebase, preventing them from being hardcoded. Python provides straightforward ways to interact with these variables. This article will guide you through accessing, setting, and understanding the best practices for using environment variables in your Python projects.
Reading Environment Variables with os.environ
The primary way to interact with environment variables in Python is through the built-in os
module, specifically the os.environ
dictionary-like object. This object provides access to all environment variables available to the current process.
import os
# Accessing an environment variable
api_key = os.environ.get('MY_API_KEY')
if api_key:
print(f"API Key found: {api_key}")
else:
print("MY_API_KEY not set.")
# Accessing all environment variables (for inspection)
# for key, value in os.environ.items():
# print(f"{key}={value}")
# Accessing a variable that might not exist (with default value)
debug_mode = os.environ.get('DEBUG_MODE', 'False').lower() == 'true'
print(f"Debug Mode: {debug_mode}")
Basic usage of os.environ.get()
to read environment variables.
os.environ.get('VAR_NAME', default_value)
instead of os.environ['VAR_NAME']
. Using get()
prevents a KeyError
if the variable is not set, allowing you to provide a sensible default or handle its absence gracefully.Setting Environment Variables
While it's generally recommended to set environment variables outside your Python script (e.g., in your shell, deployment configuration, or .env
files), you can also set them programmatically within your Python code. This can be useful for testing or for child processes.
import os
# Setting a new environment variable
os.environ['NEW_VARIABLE'] = 'Hello World'
print(f"NEW_VARIABLE: {os.environ.get('NEW_VARIABLE')}")
# Modifying an existing environment variable
os.environ['PATH'] = os.environ.get('PATH', '') + os.pathsep + '/usr/local/custom_bin'
print(f"Updated PATH: {os.environ['PATH']}")
# Deleting an environment variable
if 'NEW_VARIABLE' in os.environ:
del os.environ['NEW_VARIABLE']
print("NEW_VARIABLE deleted.")
else:
print("NEW_VARIABLE was not set.")
Setting and deleting environment variables programmatically.
os.environ
are only valid for the current Python process and any child processes it spawns. They do not affect the parent shell or other processes on your system.Managing Environment Variables with .env
Files
For local development, managing numerous environment variables directly in your shell can become cumbersome. The python-dotenv
library provides a convenient way to load variables from a .env
file into os.environ
.
First, install the library:
pip install python-dotenv
Installing the python-dotenv
library.
Next, create a .env
file in your project's root directory:
# .env file
DATABASE_URL="postgresql://user:password@host:port/dbname"
SECRET_KEY="supersecretkey123"
DEBUG=True
Example .env
file content.
Then, load these variables in your Python script:
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Access variables as usual
db_url = os.environ.get('DATABASE_URL')
secret_key = os.environ.get('SECRET_KEY')
debug_mode = os.environ.get('DEBUG', 'False').lower() == 'true'
print(f"Database URL: {db_url}")
print(f"Secret Key: {secret_key}")
print(f"Debug Mode: {debug_mode}")
Loading environment variables from a .env
file using python-dotenv
.
.env
file to your .gitignore
to prevent sensitive information from being committed to version control.Environment Variable Access Flow
The following diagram illustrates the typical flow of how environment variables are accessed and managed in a Python application.
flowchart TD A[Start Application] --> B{Load Environment Variables} B --> C[System/Shell Variables] B --> D[.env File (if used)] C -- Overridden by --> D D --> E[Python os.environ] E --> F{Application Logic} F --> G[Use Config Values] G --> H[End Application]
Flowchart illustrating how environment variables are loaded and accessed in a Python application.
Best Practices for Using Environment Variables
To ensure your applications are secure, maintainable, and flexible, follow these best practices when working with environment variables:
1. Separate Configuration from Code
Never hardcode sensitive information or environment-specific settings directly into your source code. Use environment variables for API keys, database URLs, and other credentials.
2. Use Descriptive Names
Name your environment variables clearly and consistently (e.g., DATABASE_URL
, API_KEY
, DEBUG_MODE
). Use uppercase letters and underscores, which is a common convention.
3. Provide Default Values
Always use os.environ.get()
with a default value or implement robust error handling for critical variables to prevent application crashes if a variable is missing.
4. Secure Sensitive Data
Ensure .env
files are not committed to version control (add them to .gitignore
). In production, use secure secrets management services provided by your cloud provider (e.g., AWS Secrets Manager, Azure Key Vault, Google Secret Manager) or orchestration tools (e.g., Kubernetes Secrets).
5. Type Conversion
Environment variables are always strings. Convert them to the appropriate data type (e.g., int()
, bool()
) in your Python code as needed. For booleans, check for specific string values like 'true'
or '1'
.