Environment variables are a convenient way to store system-level information that can be accessed by multiple applications.
They play a crucial role in providing a secure, flexible and scalable infrastructure for your applications.
In this tutorial, we’ll explore how you can access environment variables in Python.
We’ll start with a brief overview of what environment variables are, why you should use them and the different ways you can access them in Python.
What are Environment Variables?
Environment variables are a set of key-value pairs that are stored in the operating system and can be accessed by applications running on that system.
They provide a convenient way to store and share information between different parts of an application or between multiple applications.
Why Use Environment Variables?
Using environment variables instead of hard-coded values has several advantages. They allow you to:
- Store sensitive information such as passwords, API keys, and other secrets that should not be hard-coded in your source code.
- Configure your application differently in different environments such as development, testing and production.
- Make it easier to manage and update configuration information without having to modify and recompile the source code.
How to Access Environment Variables in Python
There are several ways to access environment variables in Python. Let’s explore the most commonly used methods:
1. os.environ
The os
module provides a dictionary-like object called os.environ
that can be used to access environment variables in Python.
Here’s an example of how you can access the value of an environment variable using os.environ
:
import os print(os.environ['MY_VAR'])
In this example, we’re accessing the value of the MY_VAR
environment variable. If the variable does not exist, a KeyError
will be raised.
To avoid this, you can use the get
method of os.environ
, which returns None
if the variable is not found:
import os my_var = os.environ.get('MY_VAR') if my_var: print(my_var) else: print('MY_VAR is not set')
2. python-dotenv
The python-dotenv
library provides a simple way to load environment variables from a file into the os.environ
dictionary.
This can be useful when you want to store environment variables in a separate file, instead of setting them directly in the operating system.
Here’s an example of how you can use python-dotenv
to load environment variables:
import os from dotenv import load_dotenv load_dotenv() print(os.getenv('MY_VAR'))
In this example, we’re using the load_dotenv
function to load environment variables from a file named .env
.
The .env
file should be stored in the same directory as your Python script and should contain the environment variables in the following format:
MY_VAR=value
Conclusion
In this post, we explored how to access environment variables in Python using the os.environ
dictionary and the python-dotenv
library.
We saw that using environment variables is a convenient and secure way to store system-level information that can be accessed by multiple applications.