What you Should Know About Environment Variables (This Could Save Your Project)

Gavin Wiener
4 min readNov 7, 2020

From my experience with my students and new programmers, environment variables are a highly overlooked concept. It does not matter if you’re working with Python, Django, Ruby, etc, it just so happens that Python and Django are my expertize.

I literally had some say to me on Twitter the other day,

“To validate your point, I don’t even know what environment variables are! “

We’re going to discuss;

  • What are Environment Variables
  • How are Environment Variables Used
  • Benefits of Using Environment Variables
  • How to use Environment Variables in Your Projects
  • An Important Warning

What are Environment Variables

Environment variables do not only apply to Django projects. Environment variables start in your operating system.

The definition of an environment variable

“An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer.

They are part of the environment in which a process runs…”

Source: https://en.wikipedia.org/wiki/Environment_variable

For me, the first two sentences of this article are the most important, and you’ll see why later.

You’ve probably experienced environment variables and you do not realize it. If you type `env` in your Linux terminal, or `set` in your Windows terminal, it will show you a list of various environment variables.

These environment variables help our operating system to work, and we can use environment variables to help our programs and projects to work.

How are Environment Variables Set and Used

This is a quick-start. I recommend watching this video for more comprehensive examples (otherwise this email is going to turn into a book).

Windows

Set an environment variable
set VAR_NAME=”VALUE”

Fetch an environment variable
echo %VAR_NAME%

Linux

Set an environment variable
export VAR_NAME=”VALUE”

Fetch an environment variable
echo $VAR_NAME

Benefits of Environment Variables

This is the meat-and-potatoes. Why do we use environment variables (in our code). The two focuses are security, and flexibility

Hiding Confidential information

Having confidential credentials or information in your code-base is a big no-no. This is known as “hard-coding”. For example;

MY_API_KEY = “This is my API key”

If you hard-code confidential credentials in your code-base, you cannot safely open-source or share your code-base. The reason is obvious, anyone can use your credentials, impersonate you, and access additional confidential information. If it’s an API key for a free weather service, maybe that is not the biggest problem, but if it’s the credentials to your Stripe account…

This does not only apply if you are open-sourcing or sharing your code-base…
What if access to your code-base gets compromised?
Someone’s laptop with the code-base gets stolen?
What if your GitHub/BitBucket gets hacked?
Etc.

You’d have to reset all your credentials.

Flexibility

Regarding open-source and sharing your code-base, environment variables allows flexibility. Firstly, you can safely share the code-base. Secondly, instead of having to go through the code-base and change all the necessary credentials, no code changes are necessary to. The variables would be set outside of the codebase. Potentially, this would also allow less technical users are of your project or program to run it.

Your deployments also benefit. Using environment variables allows you to set different conditions without changing any of the code. For most larger projects, you will have at least a development, testing, and production environment. If you had to change the settings each time, it would be a headache, and you will almost certainly make a mistake one day. A good example is database settings, you will definitely be using a different base for development, testing, and production.

This helps continuous integration significantly. When you save the same code into your version control, you can run automated tests and production deployments with the exact same code you use during development, but you are just changing the environment variables.

How to use Environment Variables in Your Projects

Again, this video is a useful example,

Using environment variables In Python is easy. You need just the OS module, OS documentation

import os

And to access them we need to know just the variable name used earlier in “How are Environment Variables Set and Used” section.

For example, earlier we set `VAR_NAME` as an environment variable.

To access it in our Python, Django etc. projects and scripts, it is simply;

VAR_NAME = os.environ[“VAR_NAME”]

Or…

VAR_NAME = os.environ.get(“VAR_NAME”)

The difference is; os.environ is just a dictionary, if you use square brackets, the environment variable must be set or your program will not run. Using .get(<var_name>, default=None) allows you set a default. The default is None.

Important Final Warning

You must use environment variables from the very start of your project. If you do not, and you use version control as well e.g. Git, the confidential credentials will be in your version control history if you originally hard-coded them.

You can fix this but it becomes a lot more of a headache.

Sign-up here to receive automation tips, case studies, and guides on the mistakes you maybe making on your automation journey.

And follow me on Twitter where I post about my journey, automation, and business growth.

--

--

Gavin Wiener

I'm a coder and freelancer from South Africa with 5+ years experience in automations, integrations and API's using Python and Django.