Welcome to the python mini-series. In the mini-series, I will cover some small pieces of python which I found useful in the past. Maybe this is also useful for you.

In this article, I will explain how to access environment variables in python code.

Environment variables are variables which can be set in the current running “environment”. This environment is usually the current operating system in which a program is executed. Processes/programs executed within such an environment can access these variables.

Such variables are usually used for configuring the processes/programs or storing information outside of such entities. 

If you are completely new to environment variables have a look at this Wikipedia entry. It gives quite a good introduction on the topic and how they are used in different OS.

Environment variables are used to store information that is ideally not hardcoded or not supposed to be shared in the code of a program. Good examples of such information would be login credentials for an API, a directory path that changes depending on where the program is executed or a path to a specific library in the OS. 

How to set environment variables?

Depending on the OS the setting of an environment variable is done differently. I will just cover basic Unix based systems and Windows here. For the rest you will need to google, as the main point of this post is to show how to access these variables in the code. 


  1. Go to the command line/cmd
  2. Execute the command set VARIABLE=value to create a temporary environment variable (example: set JONNYSTEST=”This is the value of the test var”)
  3. With the command set without any parameters, you will get a list of all current environment variables. If the command was successful you should find your created variable in the result list.

Unix based systems:

Disclaimer: In Unix based or Unix like systems
the way the variables are set differ depending on the shell which is used.
In this example, I cover only ksh or bash and related shells.
I usually only use bash.. 
  1. Go to the shell / bash
  2. Execute the command export VARIABLE=value (Example export TSTLNX=’This is a test text’)
  3. To test if the variable has been set you can execute echo $. This will return the value of the variable if it was set properly.

How to access the environment variables in your python program?

If the variables have been set directly you can simply use the following code to access the variables.

#import os library
import os
#read environment variable
test_var = os.getenv('TESTLNX')
#print result
print('Here the value of our test environment variable: '+test_var) 

Here the breakdown of the code:

  1. import os <= import the python package os. It contains Miscellaneous operating system interfaces. The official python documentation can be found here. The package provides a portable way to access operating system dependent functionality. For example, if you want to open a file, or navigate to a path.
  2. Test_var = os.getenv(‘TSTLNX’) <= Return the value of the environment variable key if it exists, or default if it doesn’t. key, default and the result are str.
  3. print(‘Here the value of our test environment variable: ‘+test_var) <= print the value of the variable to the screen/bash

That’s it! Pretty simple and very useful ;)

If you would like access to the example script you can get if from my bitbucket repo here.