7 min readManaging Environment Variables like a boss

crab spider, variable, spider-4390424.jpg

Before starting with this blog, I guess you already know what environment variables mean. Or for better understanding let me quote what Wikipedia says about it.

Overview

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. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

So to simplify this in computing, a variable is a word that stands for a value that can change. Environment variables are special variables (like $HOME) that contain information about your login session. They’re stored for the system shell to use when executing commands. They exist whether you’re using Linux, Mac, or Windows. Many of these variables are set by default during installation or user creation.

Photo by Chris Ried on Unsplash

I’m not going to cover why the environment variable is important or what’s the usage of it, because that’s not what the headline says. So let’s dive into the content.

How to View Environment Variables?

It’s very easy to access environment variables from any program or any operating system. So first let’s check how you can access environment variables in the operating system. I’m using mac/UNIX so let’s check that out.

Just type env

Or you can type printenv

Easy right!

Photo by Pablo Arroyo on Unsplash

So most of the time you don’t want to see the whole list, you only want to check a single environment variable. There are many ways of it.

printenv HOME

echo $HOME

printenv | grep HOME

Photo by Josh Rakower on Unsplash

Set Shell Environment Variable

There are n number of ways to set a environment variable in a shell or for a program.

First way

export FIRST_ENV=ANAND

Simple way is to use export command and don’t use space in between = and value. And always use CAPS for key of environment variable.

Now let’s write a simple python program to print environment variable

main.py

import os
print(f"Here is your env variable:{os.environ['FIRST_ENV']}")

Second way

<ENV_NAME≥=<ENV_VALUE> python command

If you set environment variable like above then they are only set for the command above not for the shell or for the whole session.

Third way

Store all the environment in a file let’s say env_file

env_file

FIRST_ENV=ANAND
SECOND_ENV=TRIPATHI

Now create a file name run_with_env.sh

run_with_env.sh

ENV_FILE="$1"
CMD=${@:2}

set -o allexport
source $ENV_FILE
set +o allexport

$CMD

Now run the python file using the command below

sh run_with_env.sh env_file python3 main.py

Or you can also give the permission 755 to run_with_env file so that it will become executable by following command

chmod 755 run_with_env.sh
# Now run the command using executable file
./run_with_env.sh env_file python3 main.py

Fourth way

Using python-dotenv library

Install library using below command

pip install python-dotenv

Now change your python code like below

And you just have to run your code like normal python file

You can see we have mention the python file path in it. If you want to make it more easier then just put all your environment variables inside .env file. Then you don’t have to specify the path also. dotenv library bydefault reads from .env file

Fifth way

There is another wonderful way of managing environment variables for your production application i.e. by using pydantic library. I have wrote a whole blog for that. Have a look into that

Configuration management in Python like a Boss(Pydantic with python-dotenv)Note: For non-members, this article is also available at…medium.com

Set Global environment variable

There are numerous ways to set global environment variable. This comes handy for local development. Because sometimes we don’t want to set some set of environment variables everytime when we open our terminal. One easy way is to set that in ~/.bash_profile as that file runs first in shell.

If you are using zsh then set the variables in ~/.zshrc as it run before the shell commands

Conclusion

I hope you like the mini blog on environment variables. Let me know how you are setting environment variable for your programs. I personally liked the ./run_with_env.sh way.

If you liked this story. Then you can buy me a coffee using this link. That will motivate me to write more blogs like this. Peace out.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.