Environment variables in virtual environment using python

In this article we shall see what are environment variables in python, how to use them and why we should use them. They will come in handy if you want to hide sensitive information from your code.

What are environment variables?

Why it is used?

Environment variables are commonly used for the following reasons.

  1. To hide sensitive information from your code. (API keys etc)
  2. To make your code less vulnerable to hacks and other attacks.
  3. It is the right thing to do.

Use cases

I use environment variables in my Django project where the third party API keys can be hidden using the environment varibles.

How to set environment variables?

First create a virtual environment. After creating the virtual environment go to the same directory as your virtual environment.

Run the following command to open and edit the virtualenv file.

sudo nano virtualenv/bin/activate

This will open your virtual environment config file. Add the environment variables there like this.

export AMAZON_KEY='amdksnalns2182zAsifqip1\212'
export NAME='pythoncoders'
export PASSWORD='password'

Note: Make sure there is no whitespace between the key and the = symbol and the value.

Save and close the file, deactivate the virtual environment and then activate it again.

source virtualenv/bin/activate

How to access the environment variables?

The environment variables can be accessed by using the environ instance from the os module.

if you want to see all the environment variables available just do the following steps.

>>> import os
>>> os.environ

This will give us all the set environment variables.

 'COLORTERM': 'truecolor',
 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus',
 'DESKTOP_SESSION': 'ubuntu',
 'DISPLAY': ':0',
 'GDMSESSION': 'ubuntu',
 'GJS_DEBUG_OUTPUT': 'stderr',
                     'ERROR;JS '
 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated',
 'GTK_IM_MODULE': 'ibus',
 'HOME': '/home/ihritik',
 'JAVA_HOME': '/opt/jdk-10.0.1',
 'JOURNAL_STREAM': '9:28586',
 'JRE_HOME': '/opt/jdk-10.0.1/jre',
 'LANG': 'en_IN',
 'LANGUAGE': 'en_IN:en',
 'LESSCLOSE': '/usr/bin/lesspipe '
              '%s '
 'LESSOPEN': '| '
             '/usr/bin/lesspipe '
 'LOGNAME': 'ihritik',
 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:
 'PWD': '/home/ihritik',
 'QT4_IM_MODULE': 'xim',
 'QT_IM_MODULE': 'ibus',
 'SESSION_MANAGER': 'local/hritik:@/tmp/.ICE-unix/1127, unix/hritik:/tmp/.ICE-unix/1127',
 'SHELL': '/bin/bash',
 'SHLVL': '2',
 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh',
 'TERM': 'xterm-256color',
 'TEXTDOMAIN': 'im-config',
 'TEXTDOMAINDIR': '/usr/share/locale/',
 'USER': 'ihritik',
 'USERNAME': 'ihritik',
 'VTE_VERSION': '4804',
 'WAYLAND_DISPLAY': 'wayland-0',
 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/etc/xdg',
 'XDG_MENU_PREFIX': 'gnome-',
 'XDG_RUNTIME_DIR': '/run/user/1000',
 'XDG_SEAT': 'seat0',
 'XDG_SESSION_TYPE': 'wayland',
 'XDG_VTNR': '2',
 'XMODIFIERS': '@im=ibus',
 '_': '/usr/bin/python3'}"

If we want to access the variables we have set, we can access them like a key from a dictionary.

import os
amazon_key = os.environ['AMAZON_KEY'] # amdksnalns2182zAsifqip1\212
name = os.environ['NAME'] # pythoncoders
password = os.environ['PASSWORD'] # password

That is all we have to do. Now our keys are safe and protected in our environment variables.


Using environment variables in your python programs will make it more safe and secure. Always follow this better practice.

Happy coding!