Skip to main content

How to Set Environment Variables in Python: With Examples and Code

Environment variables are essential for configuring applications, storing sensitive data, and managing various settings across different environments. Understanding how to set environment variables in Python is crucial for developers working on various projects, from web applications to data science pipelines.


Setting Environment Variables in Python:

There are multiple ways to set environment variables in Python, each serving different purposes and offering varying levels of flexibility.


1. Using the os.environ Dictionary:

The os.environ dictionary provides a direct interface for accessing and modifying environment variables. You can set a new variable using the assignment operator:


import os


os.environ["MY_VARIABLE"] = "Value"

This code creates a new environment variable named MY_VARIABLE with the value "Value." You can access the variable's value using the key:


value = os.environ["MY_VARIABLE"]

print(value)  # Output: Value


2. Using the subprocess Module:

The subprocess module allows you to run external commands and capture their output. This is useful for setting environment variables within a subprocess.


import subprocess


subprocess.run(["export", "MY_VARIABLE=Value"], shell=True)

This command sets the MY_VARIABLE environment variable within the subprocess. However, this method requires shell execution and might pose security risks.


3. Using the venv Module (Virtual Environments):

Virtual environments provide isolated environments for managing project dependencies. You can set environment variables within a virtual environment using the venv module.


import venv


venv.create("my_venv", with_pip=True)


# Activate the virtual environment

# (Instructions vary for different operating systems)


# Set the environment variable

os.environ["MY_VARIABLE"] = "Value"


# Use the environment variable within the virtual environment

This approach ensures that the environment variable is only available within the virtual environment and doesn't affect the global system environment.


4. Using the dotenv Package:

The dotenv package offers a convenient way to load environment variables from a file. This is particularly useful for managing sensitive data or configuration settings.


import os

from dotenv import load_dotenv


# Load environment variables from a file named ".env"

load_dotenv()


# Access the environment variable

value = os.environ["MY_VARIABLE"]

print(value)

This approach simplifies the process of loading and managing environment variables from a centralized file.


Real-Life Use Cases:

1. Storing API Keys and Secrets:

Environment variables are ideal for storing sensitive information such as API keys and secrets. These variables can be loaded directly from the environment without hardcoding them in your code, improving security.


2. Managing Database Credentials:

Database connection details like username, password, and host are often stored in environment variables. This allows for easy configuration changes and avoids exposing credentials directly in your code.


3. Configuring Application Settings:

Environment variables can be used to configure various application settings like logging levels, debug modes, or file paths. This allows for flexible configuration adjustments based on deployment environments.


4. Enabling Feature Toggles:

Environment variables can control feature flags, enabling or disabling specific features based on conditions or user roles. This allows for staged rollouts and controlled experimentation.


Code Examples:

Here are some code examples demonstrating the use of environment variables in practical scenarios:


1. Accessing an API key from a .env file:


import os

from dotenv import load_dotenv


load_dotenv()


api_key = os.environ["API_KEY"]


# Use the API key to make API requests


2. Configuring database credentials:


import os


db_host = os.environ["DB_HOST"]

db_user = os.environ["DB_USER"]

db_password = os.environ["DB_PASSWORD"]


# Connect to the database using the credentials


3. Enabling debug mode based on an environment variable:


import os


debug_mode = os.environ.get("DEBUG", False)


if debug_mode:

    # Enable debugging features


Conclusion:

Environment variables provide a powerful mechanism for managing configuration and sensitive data in Python applications. Understanding the different methods and implementing them effectively can enhance the security, flexibility, and maintainability of your projects.

Comments

Archive

Show more

Topics

Show more