How do I access environment variables from Python?

Amit Yadav Source

I set an environment variable that I want to access in my Python application. How do I get this value?



answered 7 years ago andrei1089 #1

You can access to the environment variables using

import os
print os.environ

Try to see the content of PYTHONPATH or PYTHONHOME environment variables, maybe this will be helpful for your second question. However you should clarify it.

answered 7 years ago Jim Brissom #2

As for the environment variables:

import os
print os.environ["HOME"]

I'm afraid you'd have to flesh out your second point a little bit more before a decent answer is possible.

answered 7 years ago Rod #3

Environment variables are accessed through os.environ

import os

Or you can see a list of all the environment variables using:


As sometimes you might need to see a complete list!

# using get will return `None` if a key is not present rather than raise a `KeyError`

# os.getenv is equivalent, and can also give a default value instead of `None`
print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))

Python default installation on Windows is C:\Python. If you want to find out while running python you can do:

import sys

answered 6 years ago Scott C Wilson #4

The original question (first part) was "how to check environment variables in Python."

Here's how to check if $FOO is set:

except KeyError: 
   print "Please set the environment variable FOO"

answered 6 years ago lgriffiths #5

To check if the key exists (returns True/False)

"HOME" in os.environ

or (removed from python 3.x)


You can also use get() when printing the key; useful if you want to use a default. ( for python 2.7.3 )

print os.environ.get('HOME','/home/username/')

where /home/username/ is the default

answered 1 year ago I Am Batman #6

If you are planning to use the code in a production web application code,
using any web framework like Django/Flask, use projects like envparse, using it you can read the value as your defined type.

from envparse import env
# will read WHITE_LIST=hello,world,hi to white_list = ["hello", "world", "hi"]
white_list = env.list("WHITE_LIST", default=[]) 
# Perfect for reading boolean
DEBUG = env.bool("DEBUG", default=False)

NOTE: kennethreitz's autoenv is a recommended tool for making project specific environment variables

answered 12 months ago Azorian #7

import os
for a in os.environ:
    print('Var: ', a, 'Value: ', os.getenv(a))
print("all done")

That will print all of the environment variables along with their values.

answered 1 month ago Kardi Teknomo #8

In Python 3:

import os
for param in os.environ.keys():
    print("%s: %s " % (param, os.environ[param]))

answered 2 weeks ago britodfbr #9

Actualy may be this away:

import os

for item, value in os.environ.items():
    print('{}: {}'.format(item, value))

Or simplely:

for i, j in os.environ.items():
    print(i, j)

for view the value in the parameter:




to set the value:

os.environ['HOME'] = '/new/value'

comments powered by Disqus