How to Use Environment Variables with Python Dotenv

How to Use Environment Variables with Python Dotenv

We often have to use API keys, secret keys, identity keys, and so on when we build applications. Each of these keys comes with a warning not to show it publicly or to lose it. Managing these keys is not only a safety concern, but also a matter of staying up to date as keys have to change from time to time.  That’s where Python dotenv comes into play.

Clearly, we shouldn’t be storing these keys directly in our code base. How should we be storing these keys? Currently, the best practice is to use environment variables. In this post we will be learning about:

What are Environment Variables?

Py Dotenv Manages environment variables

Environment variables are variables that we store in the programming environment that we use. Python practitioners should be familiar with virtual environments. Virtual environments manage things such as packages and your Python version. You can think of these packages and versions as environment variables for your virtual environment.

In terms of programming, environment variables are variables that are specific to your programming environment. Typical examples include your usernames and passwords, your identity and access keys, and anything else that is specific to your local programming setup.

When do We Use Environment Variables in Python?

We should use environment variables for Python anytime we’re creating a program that uses variables that are specific to our usage. We especially want to use environment variables if we’re going to upload our code to Git or anywhere online. Using environment variables keeps our secret keys secret through being stored in a .env file, which should be, and usually is automatically, added to the .gitignore file.

What is Python Dotenv?

The dotenv package is a common package in many programming languages including Python. The Python version of the package is called python-dotenv. The Python Dotenv module allows you to not only retrieve the values stored in a .env file easily, but also allows for finding these values from anywhere in your application.

How to Use Python Dotenv to Manage Environment Variables

The python-dotenv library provides two main functions to manage environment variables, load_dotenv and dotenv_values. The load_dotenv function from Python Dotenv loads the key-value pairs in the .env file as environment variables that can be accessed using os. The dotenv_values function returns the key-value pairs as a dictionary.

Here’s how to use the dotenv_values function. Provide a filename to the dotenv_values function and store that in a variable.

​​from dotenv import dotenv_values

config = dotenv_values(".env")  # config = {"USER": "foo", "EMAIL": ""}

Here’s how to use Python dotenv‘s load_dotenv function to load your environment variables through the os module. After calling load_dotenv(), you can access your environment variables through using os.getenv().

from dotenv import load_dotenv
print(os.getenv(“USER”)) # should be “foo”

Why Manage Environment Variables with Python Dotenv?

Python Dotenv is not the only way to manage environment variables. Technically, you can use the os module to access the .env file and get environment variables without installing the python-dotenv package. However, that’s not nearly as pretty. You would have to find, open, and parse the file yourself.

Summary of Using Environment Variables with Python Dotenv

Environment variables are used to dictate how a program runs in a given environment. They help us keep secret keys secret. Using environment variables is common in all sorts of application programming. It’s so common that python-dotenv is just the Python version of the dotenv package.  Javascript, Java, R and more languages also have their own dotenv packages.

We can use Python Dotenv to load the variables in the .env file not only as environment variables, but also as a dictionary. Then, we can access the loaded environment variables either through the OS or through the dictionary values.

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

Leave a Reply

%d bloggers like this: