Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 979 Bytes

46.md

File metadata and controls

37 lines (26 loc) · 979 Bytes
rank vote view answer url
46 2177 1871474 11 url

在 Python 中如何获取环境变量


环境变量可以通过 os.environ 来获取

import os
print(os.environ['HOME'])

或者你可以查看一下所有环境变量的列表:

os.environ
# using get will return `None` if a key is not present rather than raise a `KeyError`
print(os.environ.get('KEY_THAT_MI¡GHT_EXIST'))

# os.getenv is equivalent, and can also give a default value instead of `None`
print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))

Python default installation 在 Windows 中是 C:\Python, 如果你想在运行时得到可以运行下面的:

import sys
print(sys.prefix)