rank vote view answer url
64 1361 1130319 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)
Copyright © taizilongxu 2018 all right reserved,powered by Gitbook该文件修订时间: 2018-10-17 17:33:54

results matching ""

    No results matching ""