Skip to content

Using Environment Variables in Python

Environment Variables in Python Cover Image

In this post, you’ll learn about how to use environment variables in Python on Windows, macOS, and Linux. Environment variables in Python enable you to write smoother workflows and more secure code.

You’ll learn why environment variables are useful to learn and how to implement them. You’ll learn how to get and set environment variables in Python. You’ll also learn how to implement Python environment variables safely in source control management tools like Github.

By the end of this tutorial, you’ll have learned:

  • What environment variables in Python are and why you’ll want to use them
  • How to get and set environment variables in Python using Windows, macOS, and Linux
  • How to improve the environment variable workflow with the dotenv library
  • How to safely store environment variables when working with source control management tools like Github

What Python Environment Variables Are

Python environment variables are variables that exist outside of your code and are part of your system’s or current user’s configurations. Why would you want to use environment variables? Environment variables provide the opportunity to make your code:

  • More streamlined – by not needing to repeat constants such as API keys across multiple files. Similarly, using environment variables gives you the opportunity to configure your code so it can be tailored to a particular user running your code. For example, if your code is used by multiple people, the user’s path can be accessed from the environment.
  • More secure – by not exposing secure keys or user configurations within your code. This allows you to share code that relies on API keys or other secure information without needing to expose that code. This prevents others from ready these secure pieces of information.

Environment variables represent key-value pairs of data that can be accessed by your code. As you’ll learn later, they can be accessed just like any dictionary value.

When To Use Python Environment Variables

Environment variables should be used when a variable changes with the environment (such as when a different user runs the code). This means that the best use cases are when the code would otherwise need manual updating when run in a different environment.

Similarly, environment variables should be used when secure data are implemented in a shared piece of code (or code shared to a source control management tool like Github). This allows you to embed secure information outside the code, while still being able to run your code.

When you first learn about environment variables, it may seem like a good idea to use them for everything. Following the guideline mentioned above sets a good standard for when to use them.

How to Get Environment Variables in Python

Environment variables can be accessed using the os library. In particular, they’re stored in the environ attribute. Because the os package is part of the standard Python library, you don’t need to install anything extra in order to get it to run.

In a later section, you’ll learn how to use the dotenv module to manage environment variables more easily. For now, let’s take a look at how you can get all environment variables using the os library.

How to See All Python Environment Variables

The easiest way to get all the environment variables available is to print out the os.environ attribute. In order to do this, you’ll need to first import the library. Let’s see what this looks like:

# Getting All Environment Variables Using os
import os
print(os.environ)

# Returns:
# environ({'TERM_PROGRAM': 'vscode', ...})

I have truncated the returned value above to make it more readable. Running this on your machine will likely display a large amount of variables.

We can check the type of this variable to see what it is:

# Checking the Type of the os.environ
import os
print(type(os.environ))

# Returns
# <class 'os._Environ'>

In the next section, you’ll learn how to get a single environment variable in Python.

How to Get a Single Environment Variable in Python

Because the returned value of the os.environ attribute is a dictionary-like structure, we can access a particular variable by using its key. For example, if we wanted to get the environment variable for 'USER' we could access it as we would any other dictionary’s value:

# Getting a Single Environment Variable in Python
import os
print(os.environ['USER'])

# Returns: datagy

Now, what would happen if we tried to access a variable that didn’t exist? Let’s try getting the environment variable for 'nonsense':

# Getting an Environment Variable that Doesn't Exist
import os
print(os.environ['nonesense'])

# Returns: KeyError: 'nonsense'

We can see that this raises a KeyError. If we don’t want our program to crash, we can use the .get() method to safely return None if no value exists. Let’s see what this looks like:

# Returning None if No Environment Variable Exists
import os
print(os.getenv('nonsense'))

# Returns: None

In the next section, you’ll learn how to check if an environment variable exists in Python.

How to Check if an Environment Variable Exists in Python

Because the returned value from the os.environ attribute is dictionary-like, you can use the in keyword to check whether or not an environment variable exists in Python. Let’s see how we can check if the variable 'USER' exists on the environment using an if-else block.

# Checking if an Environment Variable Exists in Python
import os

if 'USER' in os.environ:
    print('Environment variable exists!')
else:
    print('Environment variable does not exist.')

# Returns:
# Environment variable exists!

Using this conditional allows you to safely run code to see if an environment variable exists. If the variable doesn’t exist, you could, for example, prompt the user for input before continuing.

In the following section, you’ll learn how to return a default value if one doesn’t exist.

How to Return a Default Value for Environment Variables If One Doesn’t Exist

If no value exists for an environment variable, you may want to pass in a default value. This can also be done by using the .getenv() method. By passing in a second parameter, a default value can be returned if a variable doesn’t exist.

Let’s see what this looks like:

# Returning a Default Value When a Variable Doesn't Exist
import os
print(os.getenv('nonsense', 'default value'))

# Returns: default value

In the next sections, you’ll learn how to set environment variables.

How to Set Environment Variables in Python

Now that you know how to get environment variables using Python, in this section, you’ll learn how to set the values for new or existing variables. Because this process is different for Windows and macOS / Linux, the process is split across two different sections.

How to Set Environment Variables in Python Using macOS And Linux

To set an environment variable in Python using either macOS or Linus is to use the export command in a shell session. For example, if we wanted to set the variable of API_KEY to be equal to '123acb', we could write the following:

# Setting an Environment Variable
export API_KEY = '123abc'

When you run this code in your terminal, the environment variable will be set globally for all programs for that session. When you close your terminal, the environment variables are lost again.

If you only wanted to set the environment variable for a particular script, you can do this as well from the shell command. Simply change the command to this:

# Setting an Environment Variable
API_KEY = '123abc' python myscript.py

How to Set Environment Variables in Python Using Windows

Windows provides very similar ways to set environment variables in Python. Similar to macOS and Linux, you can set the variables for a single session by using the command prompt. Rather than using export, you use the word set. Let’s take a look:

# Setting an Environment Variable
set API_KEY = '123abc'

Instead, if you’re using the PowerShell console, you need to use the following code:

# Using PowerShell to Set an Environment Variable
$Env:API_KEY = '123abc'

In the following section, you’ll learn a much easier way to set and manage environment variables, using the dotenv library.

How to Use dotenv in Python to Work with Environment Variables in Python

Using the terminal or command prompt to manage environment variables can be a time-consuming and, frankly, annoying task. Because of this, Python provides the option to use the dotenv library to better manage environment variables.

The benefit of using the dotenv library is that the process is the same for Windows, macOS, and Linux. The way that this process works is by including a .env file in the project directory that you’re working in. The file will contain the environment variables that you’re hoping to use in your script.

Let’s see how we can use the dotenv library to manage our environment variables better in Python. First, we’ll need to install the dotenv library, which can be done using either pip or conda:

# Install dotenv
pip install python-dotenv
conda install python-dotenv

Once this is done, we need to create the file in the directory. The easiest way to do this is using the terminal:

touch .env

Once this is done, you can open the file, which is really just a text file. From there, you can add all of the environment variables that you want to use. The way that this is done is by placing different variables on new lines and creating the key-value pair by using an equal sign =:

API_KEY=123abc
user_name=Nik

From there, in your Python script it’s as easy as importing the library and loading the file:

# Loading an Environment Variable File with dotenv
from dotenv import load_dotenv
load_dotenv()

The function will search through the current directory for the .env file. If it doesn’t find one, then it continues to move up through the directories to try and find the file.

Similarly, you could simply pass in the full path to the file to make your code more explicit:

# Loading an Environment Variable File Explicitly with dotenv
from dotenv import load_dotenv
load_dotenv('/home/datagy/project/.env')

Using dotenv Safely with Source Control Management Tools

Because environment variables can be used to store sensitive information, it’s important to be mindful of not including them when working with SCM tools like Github. In order to work with these safely, you can add them to a .gitignore file.

In order to make your code more understandable for others, simply add an example .env file to your repository. This allows readers of your code to understand what environment variables must be set.

Conclusion

In this tutorial, you learned all about environment variables in Python. Environment variables provide the ability to elegantly and securely run your code. They provide the opportunity to streamline your code without repeating and changing your code. Similarly, they prevent having to share secure items such as API Keys.

You learned how to get and set environment variables in Python using the os library. You also learned how to use the dotenv library to make managing environment variables much easier in Python.

Additional Resources

To learn more about related topics, check out the tutorials below:

1 thought on “Using Environment Variables in Python”

  1. Questions:
    Great article – Almost covers all my questions. I am developing a python application involving an API an this seems a better way instead of encrypt/decrypt. But I initially have two questions:

    1. Page 5 “..The easiest way to do this is using the terminal. Is there a way to create the dotenv directory in python code? (mkdir)?

    2. During python application development is there a way to erase/remove the .env directory. Would this be done with a simple os.remove in Os and/or Windows?

    Thank you,
    Terry Lengel

Leave a Reply

Your email address will not be published.