如何使用 python 获取 Mac OS X 中的环境变量?
如何在 Mac OS X 中使用 python 代码查找环境变量(例如从 .profile 导出 HG_USER)?
How can I lookup environment variables (e.g. export HG_USER from .profile) using python code in Mac OS X?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
os.environ 是一个包含所有环境变量的字典。您需要
import os
才能使用它。因此,例如 HG_USER 将由 os.environ['HG_USER'] 访问。
os.environ
is a dictionary containing all the environment variables. You need toimport os
before you can use it.So, for example HG_USER would be accessed by
os.environ['HG_USER']
.使用 os 模块:
返回一个以环境变量为键的字典。
Use the
os
module:Returns a dictionary with the environment variables as keys.
使用操作系统:
Use
os
:您还可以使用辅助函数 os.getenv(varname[, value]) 它将返回环境变量 varname 的值,如果不存在将返回value 默认为
None
You can also the helper function
os.getenv(varname[, value])
which will return the value of environment variablevarname
, and if it not present will returnvalue
which defaults toNone