When you’re developing across different environments, you may want to specify different configuration values for each environment. The computer on which you developed an application will not be the same as the one on which you deploy an application.
That’s where environment variables come into use!
In this guide, we’re going to talk about what environment variables are and how they can be used. We’ll walk through an example of Python environment variables to help you get started.
What is an Environment Variable?
An environment variable is a variable whose value is set outside of a program.
Environment variables allow you to set different values for a variable depending on the environment in which you are building an application.
Why Are Environment Variables Used?
Many applications, especially collaborative ones, have multiple different environments. One environment will be used for testing, another for production, and yet another for development. These environments often have different configuration values that need to be set.
The application programming interface (API) keys that you use in production are probably not going to be the same as the ones you use on your local machine. You may use dummy data on your local machine when building an application, but this is not appropriate in a production environment.
The value of an environmental variable can be changed without altering your program. This means that you can easily deploy changes to your code without having to substitute configuration values for a new environment.
Environment variables are also used for security purposes. Writing API keys and config values inside a main program can be risky because everyone on a project can see them. If you state a value inside an environment variable, only your program and the developer who set the variable will be able to access it.
How to Set an Environment Variable
Let’s begin by creating an environment variable. An environment variable will only last until your Python session is over. This means that once you close your Python interpreter, the variable will be reset in your Python scripts.
We’re building an app that uses the Airtable API. We are going to share our code on GitHub so we don’t want our API key to be shown to the public. To add our API key to our code without sharing it publicly, we are going to use an environment variable.
The first step is to import the os library:
This library contains the code for working with Python environment variables. Next, we are going to set an environment variable:
Os.environ[“AIRTABLE_KEY”] = “YOUR_API_KEY”
This code will set a variable called AIRTABLE_KEY. Its value is
YOUR_API_KEY. Assigning an environment variable works similar to how you change values in a dictionary.
You need to specify the name of the variable you want to set or change, followed by an equals sign, then the value you want to assign to the variable.
The same syntax can be used to change an environment variable:
os.environ[“AIRTABLE_KEY”] = “YOUR_API_KEY_2”
The value of our API key has changed to
How to Retrieve an Environment Variable
We’ve just set an environment variable. The question on your mind is probably: how can we retrieve the value? That’s where we can use the
Let’s retrieve the value of our
api_key = os.environ.get("AIRTABLE_KEY") print(api_key)
We have stated that we want to get the value of the
AIRTABLE_KEY environment variable. We assign this value to a new variable called
api_key and then print it to the console.
Our code returns:
You can retrieve a list of all the environment variables you have set by printing the contents of os.environ to the console:
There are a number of default environment variables set inside Python. This means that printing out all the environment variables set inside a Python program may return a long list.
How to Delete an Environment Variable
You can delete environment variables inside a Python project. This can be accomplished using the
os.environ.pop() method. Let’s delete our
AIRTABLE_KEY environment variable:
This code removes the
AIRTABLE_KEY environment variable.
It’s impractical to do this if you want to clear multiple environment variables. If you are starting a new instance of a project, or want to reset all the environment variables in your session, you could have dozens of values to override.
That’s where the
clear() method comes in handy. Let’s remove all the environment variables in our session:
This code clears the values of all environment variables. Then, it prints a list of all the environment variables in our session:
Python sets some default environment variables which may be needed by your project.
You should only use the
clear() method if you are confident that you do not need any of the environment variables in a program anymore. Using the
pop() method is preferred because it is more specific.
How to Set Environment Variables Using Dotenv
The dotenv library provides a number of useful functions for managing environment variables.
Notably, dotenv allows you to read environment variables from a file. This means that you do not need to declare them inside a Python shell.
Reading environment variables from a module file is more convenient if you have a lot of values to read. It also makes it easier to manage variables if you need to change their values.
To work with the dotenv package, you will need to install it. You can do this using either easy_install or pip.
Let’s start by defining an environment variable. To do this, we are going to create a file called .env. We can create this file from the command line using the touch command:
Open up your .env file and add in the following contents:
Environment variables are assigned like any other variable. On the left side of the equals sign, you have the name of the variable. On the right side, you have the value that variable will store. These two values are separated by an equals sign.
Next, we’re going to load our variables into a file:
from dotenv import load_dotenv load_dotenv()
This code imports the
load_dotenv() method from the dotenv library and executes it. This will read all the variables in our .env file into our environment.
Next, let’s try to retrieve our variable using the os library:
import os api_key = os.environ.get("AIRTABLE_KEY") print(api_key)
Our code returns:
YOUR_API_KEY_3. Our API key was set inside our .env file. The
load_dotenv() method loaded our environment variables and made them accessible using the os.environ method.
Environment variables are predefined values which are used to configure a value outside of a program. They are commonly used to set up different environments, hence their name.
Environment variables are a secure way to set secret values. You would not want to directly add an API key in your application code because it would be readable to everyone who sees your application. You can set it in an environment variable so that only you and your program can see the value.
Now you’re ready to start working with environment variables in Python code like a professional!