How can I lookup environment variables (e.g. export HG_USER from .profile) using python code in Mac OS X?
Asked
Active
Viewed 1.3k times
3
-
What have you tried? Please post the code you tried and the error you're getting. – S.Lott Jan 26 '11 at 19:23
-
didn't try anything, wanted a direction – Afiku Jan 27 '11 at 07:26
4 Answers
11
os.environ
is a dictionary containing all the environment variables. You need to import os
before you can use it.
So, for example HG_USER would be accessed by os.environ['HG_USER']
.

Vlad H
- 3,629
- 1
- 19
- 13
2
Use the os
module:
import os
os.environ
Returns a dictionary with the environment variables as keys.

Michael J. Barber
- 24,518
- 9
- 68
- 88
1
You can also the helper function os.getenv(varname[, value])
which will return the value of environment variable varname
, and if it not present will return value
which defaults to None

Senthil Kumaran
- 54,681
- 14
- 94
- 131