Categories
General Python

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?
  • When do We Use Environment Variables in Python?
  • What is Python Dotenv?
    • How to Use Python Dotenv to Manage Environment Variables
    • Why Load Environment Variables with Python Dotenv?
  • Summary of Using Environment Variables with Python Dotenv

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.

​​from dotenv import dotenv_values

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

from dotenv import load_dotenv
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

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply Cancel reply