How to set environment variables in Python?

Learn how to set environment variables in python? with practical examples, diagrams, and best practices. Covers python, environment-variables development techniques with visual explanations.

Mastering Environment Variables in Python

Hero image for How to set environment variables in Python?

Learn how to effectively set, access, and manage environment variables in Python for robust and configurable applications.

Environment variables are a fundamental concept in software development, providing a way to configure applications without modifying their source code. They are crucial for managing sensitive information like API keys, database credentials, and for adapting application behavior across different deployment environments (development, testing, production). This article will guide you through the various methods of setting and accessing environment variables in Python, ensuring your applications are secure and flexible.

Understanding Environment Variables

Environment variables are dynamic named values that can affect the way running processes will behave on a computer. They are part of the operating system's environment and can be inherited by child processes. In Python, the os module provides a straightforward interface to interact with these variables.

flowchart TD
    A[Operating System] --> B{Environment Variables}
    B --> C[Python Application]
    C --> D[Access Variables (os.environ)]
    D --> E{Configure Application Behavior}
    E --> F[Secure Credentials]
    E --> G[Adapt to Environment]

How Python applications interact with environment variables

Accessing Environment Variables in Python

Python's built-in os module is the primary way to interact with environment variables. The os.environ object behaves like a dictionary, allowing you to access, set, and delete variables.

import os

# Accessing an environment variable
api_key = os.environ.get('MY_API_KEY')
if api_key:
    print(f"API Key: {api_key}")
else:
    print("MY_API_KEY not set.")

# Accessing with a default value
debug_mode = os.environ.get('DEBUG_MODE', 'False').lower() == 'true'
print(f"Debug Mode: {debug_mode}")

# Iterating through all environment variables
print("\nAll Environment Variables:")
for key, value in os.environ.items():
    if key.startswith('MY_'): # Example: filter for custom variables
        print(f"{key}: {value}")

Setting Environment Variables

There are several ways to set environment variables, depending on whether you want them to be temporary (for the current session/process) or persistent (system-wide). Python itself can also set variables for its child processes.

1. Temporarily in the Shell

For Linux/macOS, use export KEY=VALUE. For Windows Command Prompt, use set KEY=VALUE. For PowerShell, use $env:KEY="VALUE". These variables are only available for the current shell session and its child processes.

2. Persistently (System-wide)

On Linux/macOS, you can add export KEY=VALUE to your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc, ~/.profile). On Windows, use the System Properties dialog or setx command. Be cautious with system-wide variables, especially for sensitive data.

For development, using .env files with libraries like python-dotenv is highly recommended. This keeps environment-specific configurations separate from your codebase and out of version control.

# .env file content (e.g., in your project root)
# MY_DATABASE_URL="postgresql://user:pass@host:port/dbname"
# APP_SECRET_KEY="supersecretkey"

# Python code to load .env (install with: pip install python-dotenv)
from dotenv import load_dotenv
import os

load_dotenv() # take environment variables from .env.

db_url = os.environ.get('MY_DATABASE_URL')
secret_key = os.environ.get('APP_SECRET_KEY')

print(f"Database URL: {db_url}")
print(f"App Secret Key: {secret_key}")

Best Practices for Environment Variables

Proper management of environment variables is key to building secure and maintainable applications. Here are some best practices:

  1. Separate Configuration from Code: Environment variables allow you to change settings without redeploying your application.
  2. Use Descriptive Names: Choose clear, uppercase names for your variables (e.g., DATABASE_URL, API_KEY).
  3. Default Values: Provide sensible default values using os.environ.get('VAR_NAME', 'default_value') to make your application more robust.
  4. Version Control Exclusion: Ensure .env files are added to your .gitignore to prevent sensitive data from being committed to version control.
  5. Production Deployment: In production environments, use platform-specific mechanisms (e.g., Kubernetes secrets, AWS Secrets Manager, Heroku Config Vars) to manage environment variables securely.