简体   繁体   中英

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

如何在Mac OS X中使用python代码查找环境变量(例如从.profile导出HG_USER)?

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"]

你也可以使用辅助函数os.getenv(varname[, value])来返回环境变量varname的值,如果它不存在则返回默认值为None value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM