Access environment variables in Python

As an Amazon Associate I earn from qualifying purchases.

2 Min Read

Today, let’s talk about how you can access environment variables in Python. Environment variables can be useful for storing data you do not want to add to your code.

Getting started – Setting up environment variables

To access environment variables, you will first need to set environment variables! Let’s see how we can set environment variables USER and PASSWORD on each OS:

Windows

  • Open the command prompt in Windows.
  • Type: setx USERNAME MY_USER
  • Type: setx PASSWORD MY_PASSWORD
  • Open a new command prompt.
  • Type echo %USERNAME% && echo %PASSWORD%
  • If successful, you should see both values printed to the command prompt.

Linux

  • Open a new Linux terminal
  • Type: export USERNAME=MY_USER
  • Type: export PASSWORD=MY_PASSWORD
  • Verify both variables have been set by running echo $USERNAME $MY_PASSWORD
  • Note: This will only set the environment variable for the current session

macOS

  • Open a new terminal
  • Type: export USERNAME=MY_USER
  • Type: export PASSWORD=MY_PASSWORD
  • Verify both variables have been set by running echo $USERNAME $MY_PASSWORD
  • Note: This will only set the environment variable for the current session

Accessing environment variables in Python

Now that you have your environment variables set up, let’s access them in Python!

Start by importing the os module:

import os

Now, access the environment variable from os.environ using the environment variable name as the key:

>>> os.environ['USERNAME']
'MY_USER'
>>> os.environ['PASSWORD']
'MY_PASSWORD'

As you can see above, the values of the environment variables have been successfully retrieved.

Specifying a default environment variable in Python

Now that you know how to get environment variables, let’s have a look at a case where the environment variable do not exist. In this case you may want to use a default value.

Using os.environ.get(key, default) you can set a default value if the environment variable is not found.

For this example, I will use the environment variable VERSION which does not exist on my OS. I have also set the default value to 1:

>>> os.environ.get("VERSION", 1)
1

You can see above that Python outputted the value l. This is because VERSION was not set as an environment variable.

That’s all for how to access environment variables in Python! As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.

Make sure to check out these other Python tutorials 🙂

If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.

Leave a Comment

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

Subscribe to my newsletter to keep up to date with my latest posts

Holler Box