从Python访问环境变量 [英] Access environment variables from Python
本文介绍了从Python访问环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置了一个我想在我的Python应用程序中访问的环境变量。如何获取此值?
I set an environment variable that I want to access in my Python application. How do I get this value?
推荐答案
环境变量可通过 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)
在Windo上 Python默认安装 ws是 C:\Python
。如果你想在运行python时找出你可以做的:
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
这篇关于从Python访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文