CodeNewbie Community 🌱

Cover image for Set, Get, Read and Print Environment Variables in Python
Hichem MG
Hichem MG

Posted on

Set, Get, Read and Print Environment Variables in Python

Environment variables are an essential aspect of modern software development and deployment. They allow you to configure applications and manage settings dynamically, without hard-coding them into your source code.

In this tutorial, we will explore what environment variables are, how to set, get, print, and read them in Python, with practical applications and example.

Let's begin, shall we?

Table of Contents

  1. Understanding Environment Variables
  2. Set Environment Variables
  3. Get Environment Variables
  4. Print Environment Variables
  5. Read Environment Variables from Files
  6. Practical Examples
  7. Security Considerations
  8. Common Pitfalls and How to Avoid Them
  9. Conclusion

1. Understanding Environment Variables

Imagine you're a chef in a bustling kitchen. You have a recipe (your code), but the ingredients (your data and settings) might change. Environment variables are like the labels on the jars in your kitchen. They tell you what's inside without you having to open each one. They are key-value pairs maintained by the operating system, crucial for configuring your applications without hard-coding values.

In the world of software development, environment variables can be used to manage:

  • Application configurations: Database URLs, API keys, debug modes.
  • User-specific settings: User home directories, preferred languages.
  • System settings: Paths to executable files, system directories.

2. Set Environment Variables

Setting environment variables is like putting the right ingredients in your kitchen jars before you start cooking. You can do this directly in your operating system's shell or within your Python script.

In the Shell

For quick setups and testing, you can set environment variables in your terminal.

  • Windows (Command Prompt):
  set MY_VAR=HelloWorld
Enter fullscreen mode Exit fullscreen mode
  • Unix/Linux/Mac (Bash):
  export MY_VAR=HelloWorld
Enter fullscreen mode Exit fullscreen mode

In Python

When you need to set environment variables dynamically or within a script, Python's os module is your best friend.

import os

# Set an environment variable
os.environ['MY_VAR'] = 'HelloWorld'

# Verify that the variable is set
print(os.environ['MY_VAR'])  # Output: HelloWorld
Enter fullscreen mode Exit fullscreen mode

This snippet sets MY_VAR to HelloWorld, just like filling up a jar and labeling it.

3. Get Environment Variables

Retrieving environment variables is akin to checking the labels on your jars before adding ingredients to your dish.

In Python, you use the os module to fetch these values.

import os

# Set an environment variable
os.environ['MY_VAR'] = 'HelloWorld'

# Get an environment variable
my_var = os.environ.get('MY_VAR')
print(my_var)  # Output: HelloWorld

# Get an environment variable with a default value
my_var = os.environ.get('NON_EXISTENT_VAR', 'DefaultValue')
print(my_var)  # Output: DefaultValue
Enter fullscreen mode Exit fullscreen mode

By using os.environ.get, you avoid the risk of your code breaking if the environment variable isn't set, providing a safe fallback with a default value.

4. Print Environment Variables

Sometimes, you need to take stock of what's in your kitchen—checking all the jars and their labels. In the programming world, this means printing all environment variables, which can be very useful for debugging and configuration checks.

import os

# Print all environment variables
for key, value in os.environ.items():
    print(f'{key}: {value}')
Enter fullscreen mode Exit fullscreen mode

Running this script will output every environment variable currently set in your system. This can help you understand the current environment in which your Python script is running, and ensure all necessary variables are correctly configured.

5. Read Environment Variables from Files

For larger projects, managing environment variables directly in the shell or within your scripts can become unwieldy. This is where .env files come into play. They allow you to store all your environment variables in one place, which you can easily update as needed.

Using dotenv Library

The python-dotenv library makes it easy to read variables from a .env file. First, you'll need to install it:

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

Then, create a .env file in your project directory:

# .env file
MY_VAR=HelloWorld
ANOTHER_VAR=PythonRocks
Enter fullscreen mode Exit fullscreen mode

You can load these variables in your Python script like this:

from dotenv import load_dotenv
import os

# Load environment variables from a .env file
load_dotenv()

# Get environment variables
my_var = os.environ.get('MY_VAR')
another_var = os.environ.get('ANOTHER_VAR')

print(my_var)        # Output: HelloWorld
print(another_var)   # Output: PythonRocks
Enter fullscreen mode Exit fullscreen mode

This approach is particularly beneficial in environments like development and staging, where you might need to frequently change configuration values.

6. Practical Examples

Let's dive into some real-world scenarios where environment variables shine.

Example 1: Configuring a Database Connection

In many applications, you need to connect to a database. Hardcoding credentials in your source code is a bad practice due to security concerns. Instead, you can use environment variables.

import os
from dotenv import load_dotenv
import psycopg2

# Load environment variables from a .env file
load_dotenv()

# Get environment variables
db_host = os.environ.get('DB_HOST')
db_name = os.environ.get('DB_NAME')
db_user = os.environ.get('DB_USER')
db_password = os.environ.get('DB_PASSWORD')

# Connect to the database
connection = psycopg2.connect(
    host=db_host,
    database=db_name,
    user=db_user,
    password=db_password
)

print("Database connection established")
Enter fullscreen mode Exit fullscreen mode

This code connects to a PostgreSQL database using credentials stored in environment variables, making your application more secure and configurable.

Example 2: API Key Management

When interacting with third-party APIs, you often need an API key. Again, using environment variables is the way to go.

import os
from dotenv import load_dotenv
import requests

# Load environment variables from a .env file
load_dotenv()

# Get environment variables
api_key = os.environ.get('API_KEY')

# Use the API key to access a service
response = requests.get('https://api.example.com/data', headers={'Authorization': f'Bearer {api_key}'})
data = response.json()

print(data)
Enter fullscreen mode Exit fullscreen mode

This example demonstrates how to securely manage and use API keys without exposing them in your source code.

7. Security Considerations

Environment variables significantly enhance security, but only if used correctly. Here are some best practices:

  • Do not hard-code secrets: Always use environment variables for sensitive information.
  • Use .env files: Keep configuration details out of your codebase. Ensure your .env file is in .gitignore to prevent it from being pushed to version control.
  • Restrict access: Limit access to environment variables to only those who need it.
  • Encrypt sensitive data: If you're storing environment variables in cloud services, ensure they are encrypted.

8. Common Pitfalls and How to Avoid Them

While environment variables are powerful, there are some common mistakes to watch out for.

Not Handling Missing Variables

If an environment variable isn't set, trying to access it can result in errors. Always provide default values or handle exceptions.

import os

# Safely get an environment variable
db_password = os.environ.get('DB_PASSWORD')
if db_password is None:
    raise ValueError("DB_PASSWORD environment variable is not set")
Enter fullscreen mode Exit fullscreen mode

Inconsistent Environment Across Development and Production

Ensure consistency across different environments (development, staging, production). Use .env files and configuration management tools to maintain this consistency.

9. Conclusion

Environment variables are a simple yet powerful tool for configuring your Python applications. They allow you to manage settings dynamically, enhance security by keeping sensitive information out of your codebase, and ensure your applications are more portable and maintainable.

Whether you are setting, getting, printing, or reading environment variables, Python provides the tools you need to manage them efficiently.

Experiment with different scenarios and see how environment variables can streamline your development and deployment processes. Remember, the right configuration can turn a good application into a great one! Happy coding!

Top comments (2)

Collapse
 
philipnyk profile image
Philip • Edited

When working with environment variables in Python, it's essential to understand their role and how they can help manage your application's configuration dynamically. Here’s a detailed overview to guide you on setting, getting, reading, and printing environment variables, with additional tips for practical usage:

1. Understanding Environment Variables

Environment variables provide a flexible way to configure applications. They act as dynamic placeholders for configuration values, like database URLs, API keys, or even user preferences, that can change depending on the system or environment your application is running in. This eliminates the need to hard-code values into your source code, which is critical for security and flexibility.

For example:

  • Application configurations: Database connection strings, API keys, etc.
  • User-specific settings: Preferences, home directories.
  • System settings: Paths to executables, system directories, etc.

2. Setting Environment Variables

Setting environment variables allows you to configure your application's settings dynamically. You can set these variables both within your Python script using the os module or directly in the terminal/command prompt.

In the Terminal (for quick setups):

  • Windows Command Prompt:
  set MY_VAR=HelloWorld
Enter fullscreen mode Exit fullscreen mode
  • Unix/Linux/Mac Bash:
  export MY_VAR=HelloWorld
Enter fullscreen mode Exit fullscreen mode

In Python:

import os

# Setting an environment variable within the script
os.environ['MY_VAR'] = 'HelloWorld'

# Verifying the set environment variable
print(os.environ['MY_VAR'])  # Output: HelloWorld
Enter fullscreen mode Exit fullscreen mode

This approach allows you to set configuration values dynamically from within your code.

3. Getting Environment Variables

Once environment variables are set, you'll often need to access their values. The os module in Python provides an easy way to fetch them.

Example:

import os

# Getting an environment variable
my_var = os.environ.get('MY_VAR')
print(my_var)  # Output: HelloWorld

# If the environment variable doesn't exist, use a default value
my_var = os.environ.get('NON_EXISTENT_VAR', 'DefaultValue')
print(my_var)  # Output: DefaultValue
Enter fullscreen mode Exit fullscreen mode

By using os.environ.get(), you can avoid errors if the variable isn’t defined and set a fallback default value to prevent your application from crashing.

4. Printing Environment Variables

To better understand and troubleshoot the environment in which your script is running, it’s helpful to print out all available environment variables. This can provide insight into configuration settings, system paths, or potential issues with missing environment variables.

import os

# Print all environment variables in the current environment
for key, value in os.environ.items():
    print(f'{key}: {value}')
Enter fullscreen mode Exit fullscreen mode

This code iterates through all environment variables, printing their names and values, much like browsing through an Outback Steakhouse menu to confirm all the available options for your meal.

5. Reading Environment Variables from Files

Environment variables are typically stored in system-wide configuration files (like .env files) for applications. You can read these variables in Python using packages such as python-dotenv, which simplifies the process of managing environment-specific settings in development, staging, and production environments.

Collapse
 
vidmate337 profile image
ella muhan • Edited

Hey everyone!

Great topic on environment variables! 🌟 They really are the unsung heroes of dynamic application configuration. Here’s a quick rundown:

1. Understanding Environment Variables
Think of environment variables as labels on jars in a kitchen. They help you configure your app without embedding values directly into your code. They're key-value pairs managed by your operating system.

2. Setting Environment Variables
You can set them in your OS shell or directly within your Python script. For instance:

Windows Command Prompt: set MY_VAR=HelloWorld
Unix/Linux/Mac Bash: export MY_VAR=HelloWorld
In Python:
python
Copy code
import os
os.environ['MY_VAR'] = 'HelloWorld'

3. Getting Environment Variables
Retrieve them using Python’s os module:

python
Copy code
import os
my_var = os.environ.get('MY_VAR', 'DefaultValue')
print(my_var) # Output: HelloWorld

4. Printing Environment Variables
To check all your environment variables, use:

python
Copy code
import os
for key, value in os.environ.items():
print(f'{key}: {value}')

5. Reading from Files
For managing lots of variables, .env files are handy. Use the python-dotenv library to load them:

python
Copy code
from dotenv import load_dotenv
import os
load_dotenv()
my_var = os.environ.get('MY_VAR')
print(my_var)

6. Practical Examples

Database Connection: Store credentials in environment variables instead of hardcoding them.
API Keys: Keep your API keys secure and out of your source code.

7. Security Considerations

Avoid Hardcoding Secrets: Use environment variables for sensitive data.
Encrypt Data: For cloud storage, make sure your environment variables are encrypted.

8. Common Pitfalls

Missing Variables: Always provide defaults or handle errors gracefully.
Consistency: Ensure consistency across different environments using .env files.
Feel free to ask if you have more questions or need clarifications!

Also, if you're into exploring cool apps, check out VidMate for some awesome content! 🚀It is a versatile multimedia application that offers a comprehensive suite of features designed to enhance your media consumption experience. Whether you’re looking to download videos, stream content, or enjoy a variety of multimedia options, VidMate APK is the best choice.

Happy coding!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.