HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Virtual Environments and Packages Lesson

Python: Get Environment Variable

6 min to complete · By Martin Breuss

In this first lesson about environment variables, you'll learn how to inspect, add, and remove them directly from the command line.

Colorful illustration of a light bulb

Note: These lessons focus on using the Bash command language, which you'll have available if you're on a UNIX system or use WSL on Windows. If you're working directly on Windows, then setting environment variables takes less effort if done through the graphical user interface. You can read all about it in this detailed guide on configuring environment variables on Windows.

Open up your terminal and type the Bash command printenv. This will give you a list of all the current environment variables present in your system:

HOME=/Users/Martin
LOGNAME=Martin
USER=Martin
PATH=/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/bin:/bin

This example output shows you a couple of environment variables that are currently defined on your local machine. You'll probably see a different name and some additional lines in your own output.

You can check the value of each variable with echo $<NAME>. For example, to check the value of LOGNAME, you can type the following command:

echo $LOGNAME

The output you receive when running the same command will be what your LOGNAME variable points to. You can confirm this value by running printenv and looking for LOGNAME in your output.

With these two commands, you can inspect all the environment variables that are currently defined in your system. But what if you want to change, add, or remove one using Bash?

Adding And Removing Environment Variables

Using Bash in your CLI, you can add a new environment variable with the following command:

export <NAME>=<VALUE>

In this example, you'll have to replace <NAME> with the new environment variable that you want to add. You also need to replace <VALUE> with the value you want to assign to that environment variable.

For example, to add a new variable with the name DAY and the value Sunday, you would spell it out as follows:

export DAY=Sunday

After executing this command, you can see that a new variable has been added to your environment. Take a look at it using printenv and echo as described above.

In order to remove an environment variable with Bash, you'll have to call the following command:

unset <NAME>

Again, you'll have to add the actual name of the variable you want to remove instead of the placeholder <NAME>:

unset DAY

This Bash command removes the DAY variable you set before.

Try adding and removing some environment variables using these Bash commands. Remember you can always check what's happened using printenv or echo <NAME>.

However, when you are working on a project that requires a specific API key, you don't want to set your environment variables across your whole system environment. The value will be project-specific, which is why you should compartmentalize your environment variables just like you did for your dependencies by including them in your virtual environments.

Summary: Python, Get Environment Variable

You can interact with system-wide environment variables directly from your Bash command line:

  • Inspect your environment variables with printenv or echo $VARNAME
  • Add a new environment variable with export NAME=VALUE
  • Remove an existing environment variable with unset NAME In the next lesson, you'll learn how to combine the concept of environment variables with what you learned about virtual environments to create environment-specific virtual environment variables.