5 Best Ways to Convert Python Dictionaries to Environment Variables

πŸ’‘ Problem Formulation: Developers often need to convert a Python dictionary into environment variables for various applications, such as configuration settings for deployment or local development. The input is a Python dictionary, and the desired output is a set of environment variables reflecting the key-value pairs in the dictionary. This article outlines five methods by which this conversion can be achieved, utilizing different Python features and libraries.

Method 1: Using os.environ

The os module in Python provides a portable way of using operating system-dependent functionality. The os.environ is a dictionary-like object that represents the user’s environmental variables. With this method, you can directly assign new environment variables.

Here’s an example:

import os

config_dict = {'DATABASE': 'my_db', 'SERVER': 'localhost'}

for key, value in config_dict.items():
    os.environ[key] = value

Output of this code:

  • The DATABASE environment variable is now set to "my_db"
  • The SERVER environment variable is now set to "localhost"

This code snippet iterates over the key-value pairs in the config_dict and sets each as an environment variable. It’s a straightforward approach and does not depend on any third-party libraries.

Method 2: Using dotenv Library

The dotenv library allows you to load environment variables from a .env file, but it can also be used to set them programmatically from a dictionary. It is especially useful for managing app settings during development and deployment.

Here’s an example:

from dotenv import dotenv_values, set_key
from pathlib import Path

config_dict = {'DATABASE': 'my_db', 'SERVER': 'localhost'}
env_path = Path('.') / '.env'

for key, value in config_dict.items():
    set_key(env_path, key, value)

This will update or create a .env file with the key-value pairs from the dictionary.

This code snippet uses the dotenv library’s set_key function to write key-value pairs to the .env file, creating or updating them as needed.

Method 3: Using String Formatting

Environment variables can be set in the shell by using bash commands. Python can interact with the shell using the subprocess module. This method is useful for scripting deployment processes.

Here’s an example:

import subprocess

config_dict = {'DATABASE': 'my_db', 'SERVER': 'localhost'}

commands = '\n'.join([f'export {key}="{value}"' for key, value in config_dict.items()])
subprocess.run(commands, shell=True, check=True)

This will set environment variables available for subsequent commands executed in the same shell process.

This code snippet creates a string with multiple export commands and executes them in the shell, which is convenient when working with scripts.

Method 4: Using the json Module and Shell Redirection

When working with containerized applications, such as those deployed on Docker, you can feed environment variables from a JSON file. Python’s json module can convert a dictionary to a JSON string, which you can then redirect to an environment file.

Here’s an example:

import json

config_dict = {'DATABASE': 'my_db', 'SERVER': 'localhost'}

with open('env.json', 'w') as env_file:
    json.dump(config_dict, env_file)

The resulting JSON can be used with Docker’s --env-file flag or similar mechanisms.

This snippet writes the dictionary as JSON to a file, which can be used to configure environment variables in Docker or container orchestration systems that support JSON environment files.

Bonus One-Liner Method 5: Using Dictionary Comprehension with os.environ.update()

For a quick and concise way to update the current environment variables, you can use dictionary comprehension together with the update() method of os.environ.

Here’s an example:

import os

config_dict = {'DATABASE': 'my_db', 'SERVER': 'localhost'}
os.environ.update({k: v for k, v in config_dict.items()})

The environment variables are now updated with the values from config_dict.

This one-liner is an elegant and efficient way to update the environment variables in-place, utilizing Python’s comprehension capabilities along with the update() method.

Summary/Discussion

    Method 1: os.environ. Strengths: Straightforward, no extra dependencies, immediately updates the environment variables in the running process. Weaknesses: Changes are not persistent across sessions. Method 2: dotenv Library. Strengths: Good for development and storing settings in .env files. Weaknesses: Requires a third-party library and creates an external dependency. Method 3: String Formatting and subprocess. Strengths: Useful for shell scripting and deployment tasks, immediate effect in the shell. Weaknesses: Limited to the current shell session and more complex than other methods. Method 4: json Module and Shell Redirection. Strengths: Integrates with container orchestration systems, uses a standard JSON format. Weaknesses: Requires file I/O, better suited to deployment than development. Method 5: One-Liner Update. Strengths: Concise and elegant, good for quick updates within a Python script. Weaknesses: Limited impact, as environment changes do not persist beyond the script execution.