Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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

share|improve this question
1  
I was trying to find how to access environment variables in Python, but instead of getting this question or Python manual, I got "The Other Questions Site" and I'm wondering how this could be corrected. – Juha Autero Feb 18 '11 at 18:38
up vote 920 down vote accepted

Environment variables are accessed through os.environ

import os
print os.environ['HOME']

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

os.environ

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`
print os.environ.get('KEY_THAT_MIGHT_EXIST')

# 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
print sys.prefix
share|improve this answer
    
hello rod, thanks for your effective reply concerning 'default-installation'; effective in point of view to understand it quickly rather than go through the links. That’s really I appreciated :) but about my (1) question please look at the command and outputs snippet below: >>> import os >>> print os.environ['PYTHONPATH'] Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Python25\lib\os.py", line 435, in getitem return self.data[key.upper()] KeyError: 'PYTHONPATH' >>> print os.environ.get('PYTHONPATH') None >>> // PLZ to be continue...// – Amit Yadav Feb 5 '11 at 14:47
    
In a 1st way script is throwing Exception whereas with 2nd one giving None. So, is there any way to get it meaningful value or am I doing in a wrong way??? Amit. – Amit Yadav Feb 5 '11 at 14:49
2  
os.environ is a dictionary. Trying to access a key not present in the dictionary will throw a KeyError. The get method simply returns None when the key does not exists. Do you have PYTHONPATH set? Can you try with a variable such as PATH, that is guaranteed to exist? Does it return a meaningful value? – Rod Feb 5 '11 at 19:21
2  
PYTHONPATH is used to add new search path to Python (sys.path) from outside Python. Have a look at docs.python.org/using/cmdline.html#environment-variables – Rod Feb 7 '11 at 14:41
1  
Actually, os.environ, being a dictionary, may also return a value using its get method using os.environ.get("SOME_PARAMETER", "default_value") – sheba Dec 23 '15 at 16:11

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

"HOME" in os.environ

or (removed from python 3.x)

os.environ.has_key("HOME")

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

share|improve this answer
9  
You should use in instead of has_key. See stackoverflow.com/questions/1323410/has-key-or-in Also, the default kwarg name could be omitted for brevity (os.environ.get('HOME', '/home/username')). – Danilo Bargen Jul 12 '12 at 11:54

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.

share|improve this answer

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

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

try:  
   os.environ["FOO"]
except KeyError: 
   print "Please set the environment variable FOO"
   sys.exit(1)
share|improve this answer
26  
"FOO" in os.environ – erjoalgo Oct 17 '13 at 12:08

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.