Skip to content

Instantly share code, notes, and snippets.

@chris-zh
Last active November 26, 2022 12:04
Show Gist options
  • Save chris-zh/e50a8fa3884e424d3aa2 to your computer and use it in GitHub Desktop.
Save chris-zh/e50a8fa3884e424d3aa2 to your computer and use it in GitHub Desktop.
Python读取.env并设置环境变量
#注意:.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