开发者

How to fetch environment variables in Mac OS X using python?

开发者 https://www.devze.com 2023-02-07 02:12 出处:网络
How can I lookup envi开发者_高级运维ronment variables (e.g. export HG_USER from .profile) using python code in Mac OS X?os.environ is a dictionary containing all the environment variables. You need to

How can I lookup envi开发者_高级运维ronment variables (e.g. export HG_USER from .profile) using python code in Mac OS X?


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'].


Use the os module:

import os
os.environ

Returns a dictionary with the environment variables as keys.


Use os:

>>> import os
>>> print os.environ["PATH"]


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

0

精彩评论

暂无评论...
验证码 换一张
取 消