Do You Really Need Environment Variables in Python?

Do You Really Need Environment Variables in Python?

While developing projects across different environments, you may want to specify different configuration values for each environment. The local system on which you developed an application will not be the same as the one on which you will be deploying it. That’s where environment variables come into play!

In this blog, we’re going to talk about what environment variables are, why they are important, and how they can be used. We’ll walk through various examples 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. They exist outside of your code as part of your server environment. These variables allow you to set different values for a variable depending on the environment in which you are building an application.

Why Do We Need Environment Variables?

Let us see why we need environment variables, not only in Python, in any project we work on.

Automation

Many standard applications or projects that we work on have multiple different environments. One environment may be used for development, another can be used during testing, and yet another can be used for production. These environments have different configurations set as per the requirement.

We often use APIs in our projects, and there are API Keys associated with them. But the keys we 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.

Instead of manually changing the variables when you run a script on a different machine, you can use an environment variable that returns the value of that variable. This means you can easily deploy changes to your code without having to substitute configuration values for a new environment.

Better Security

There are various aspects of code you don't wish others to see, such as API Keys and Secrets. Instead of hardcoding these values directly into the code, if we use environment variables to store them, only your program and the developer who set the variable will be able to access it. This way, our private data is safe and more secured.

How to Set an Environment Variable in Python?

Let’s begin by creating an environment variable. This is done similarly to Python dictionaries. It’s important to note that this changes the environment variable in this session. In other words, changing the environment variable here will not affect the environment variable anywhere else.

Fire up your Python interpreter and the first step is to import the os library: :

Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os

This library contains the code for working with Python environment variables. Next, we are going to set an environment variable:

>>> os.environ["STAGE"] = "DEVELOPMENT"

How to Get an Environment Variable in Python?

The next thing we need to do is to retrieve these variables and use them in our Python code. That’s where we can use the os.environ.get()method. This retrieves the value of an environment variable currently set on your system.

>>> os.environ.get('STAGE')
'DEVELOPMENT'

You can retrieve a list of all the environment variables you have set by printing the contents of os.environ to the console:

>>> os.environ

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 in Python?

If you need to clear a single environment variable in the session you can use os.environ.pop() with the key and if you need to clear all environment variables you can use os.environ.clear()

>>> os.environ.pop("STAGE")
'DEVELOPMENT'

>>> os.environ.clear()

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.

It’s important to remember that the settings you apply in a Python script don’t work outside that specific process; os.environ doesn’t overwrite the environment variables system-wide. If you need to permanently delete or set environment variables you will need to do so with a shell environment, such as Bash.

Using Dotenv to Manage Environment Variables in Python

As the number of environment variables increase, we may need some other way to manage them. The python-dotenv library provides a number of useful functions for managing environment variables.

It 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:

>>> pip install python-dotenv

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:

>>> touch .env

Open up your .env file and add in the following contents:

STAGE=DEVELOPMENT
USER=ASHUTOSH

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 python file:

from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env file

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

user = os.environ.get('USER')
stage = os.environ.get('STAGE')

print(user, stage)

Our code returns: ASHUTOSH DEVELOPMENT. Our USER and STAGE was set inside our .env file. The load_dotenv() method loaded our environment variables and made them accessible using the os.environ method.

Like many aspects of Python, implementing environment variables isn’t cumbersome, provided you have the right tools. By making use of the os package (especially os.environ), along with dotenv when the situation calls for it, you have all the tools you need to begin using environment variables in Python.

Using Python-Decouple to Manage Environment Variables in Python

To manage our environment variables, we have another library called python-decouple. We can set the environment variables in a .env file in the same way we did before.

First install python-decouple into your local Python environment:

>>> pip install python-decouple

Now that you have installed the library and your environment variables are stored in a .env file, you can access them in your Python code like this:

from decouple import config

user = config('USER')
stage = config('STAGE')

print(user, stage)

The code will output : ASHUTOSH DEVELOPMENT. Notice that here we didn't need the os module to access the environment variables.

The benefit of using something like the above approach(whether python-dotenv or python-decouple) is that when you deploy your application to a cloud service, you can set your environment variables using whatever method or interface the provider has and your Python code should still be able to access them.

Points to Ponder

There are a few things that we need to take care of while managing our environment variables:

  • It is a common convention to use capital letters for names of global constants in your code.
  • If you’re using git, remember to add .env to your .gitignore file so that you don’t commit this file to your code repository.
  • Most cloud service providers will have a CLI or web interface that lets you configure the environment variables for your staging or production environments. For guidance in these cases you'll need to refer to their documentation on how to set environment variables when using their service.

Conclusion

Environment variables are predefined values that 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 any secret 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!

Did you find this article valuable?

Support Ashutosh Krishna by becoming a sponsor. Any amount is appreciated!