You are currently viewing Python Access Environment variable values

How to access environment variable values in Python? Environment variables are a way to store configuration values that can be accessed by applications and scripts without hardcoding them into the code. Python provides several methods to get environment variables that are os.environ, os.getenv(), and the python-dotenv package.


In this article, we will explore these different methods and provide code examples to help you learn how to access environment variables. Let’s get started!

1. Quick Examples – Access Environment Variable Values

These examples provide a high-level overview of how to access environment variables using os.environ and os.getenv(). We will discuss each of these methods in more detail and provide examples.

# Quick examples
import os

# Accessing environment variable using os.environ
database_url = os.environ.get('DATABASE_URL')
if database_url is not None:
    print(f"Database URL: {database_url}")
    print("DATABASE_URL environment variable not found")

# Accessing environment variable using os.getenv()
api_key = os.getenv('API_KEY')
if api_key is not None:
    print(f"API key: {api_key}")
    print("API_KEY environment variable not found")

2. Access Environment Variables using os.environ

Python os.environ is a dictionary-like object that contains all the environment variables set on the system. You can use it to access individual environment variables by their name or iterate over all environment variables.

To access an individual environment variable using os.environ, you can use the get() method with the name of the variable as the argument. If the variable exists, get() will return its value.

# Get the value of the PATH environment variable
import os
home_dir = os.environ.get('PATH')

If the variable doesn’t exist, get() will return None. You can also iterate over all environment variables using the items() method. This returns a list of key-value pairs, where each key is the name of an environment variable and each value is its corresponding value.

# Iterate over all environment variables
import os
for key, value in os.environ.items():

On my laptop, I get the below environment variables on the console (it’s a partial output).

python Access Environment Variables

Note that by using this we can also set the environment variable.

3. os.getenv() – Access Environment Variables Values

The os.getenv() method provided by the os module that allows you to retrieve the value of a specific environment variable by name. It works in a similar way to os.environ.get().

# Get the value of the PATH variable
import os
home_dir = os.getenv('PATH')

With using os.getenv() we can also specify the default value, so if the value doesn’t exist it will give us that default value.

# Use default value if it doesn't exist
import os
my_setting = os.getenv('MY_SETTING', 'default_value')

4. Using python-dotenv Package

This is a bit advanced method, where you will need to install a third-party package. The python-dotenv package provides a convenient way to load environment variables from a file and make them available to your Python script.

This can be useful if you have a large number of environment variables or if you need to manage them across different environments, such as development, testing, and production.

You need to first install the package:

# Install python-dotenv
pip install python-dotenv

Once you’ve installed the package, you can create a new .env file in your project directory and define your environment variables in the format NAME=VALUE.

For example: Make sure you create a file with name ".env".

# Content of my .env file

You can use the load_dotenv() function from the dotenv module to load the environment variables from the .env file into your Python script. If no path is specified, ./.env is used as the default path which means it looks for .env file in the Python script directory.

# Import dotenv
from dotenv import load_dotenv
import os

# Load the environment variables from the .env file

# Get the value of the DB_HOST environment variable
db_host = os.environ['DB_HOST']


Summary and Conclusion

We have discussed several methods to access environment variable values in Python, including using os.environ, os.getenv(), and python-dotenv. Use os.environ.items() to get all environment variables and use python-dotenv to load the environment variables from the .env file, this helps you to load different environment variables for each environment.

If you have any questions or comments, feel free to leave them below.

Happy coding!