Last active
November 26, 2022 12:04
-
-
Save chris-zh/e50a8fa3884e424d3aa2 to your computer and use it in GitHub Desktop.
Python读取.env并设置环境变量
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#注意:.env中变量的写法一般是 TEST_KEY = TEST_VALUE。最好去除每个字符两边的空格,否则加到环境变量里也读取不到 | |
def import_env(): | |
if os.path.exists('.env'): | |
print('Importing environment from .env...') | |
for line in open('.env'): | |
var = line.strip().split('=') | |
if len(var) == 2: | |
key, value = var[0].strip(), var[1].strip() | |
os.environ[key] = value | |
print(os.environ.get('MAIL_SERVER')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment