有人可以解释一下 python-dotenv 模块的使用吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41546883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can somebody explain the use of python-dotenv Module
提问by Dev Jalla
Need example realtime and please explain this module python-dotenv I am kind of confused with the documentation
需要实时示例,请解释这个模块 python-dotenv 我对文档有点困惑
回答by Will
From the Github page:
Reads the key,value pair from .env and adds them to environment variable. It is great of managing app settings during development and in production using 12-factor principles.
从 .env 读取键值对并将它们添加到环境变量中。使用 12 要素原则在开发和生产期间管理应用程序设置非常有用。
Assuming you have created the .env file along-side your settings module.
假设您已经在设置模块旁边创建了 .env 文件。
.
├── .env
└── settings.py
Add the following code to your settings.py
将以下代码添加到您的 settings.py
# settings.py
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
SECRET_KEY = os.environ.get("SECRET_KEY")
DATABASE_PASSWORD = os.environ.get("DATABASE_PASSWORD")
.env is a simple text file. With each environment variables listed per line, in the format of KEY="Value", lines starting with # is ignored.
.env 是一个简单的文本文件。每行列出每个环境变量,格式为 KEY="Value",以 # 开头的行将被忽略。
SOME_VAR=someval
# I am a comment and that is OK
FOO="BAR"