python软件是什么作用_python-dotenv的用途是什么?

本文围绕python-dotenv展开,解答其用途。它能从.env文件读取键值对并添加到环境变量,利于依据12因子原则管理开发和生产环境的应用设置。文中给出了在settings.py中使用的代码示例,还介绍了.env文件的格式。

Need an example and please explain me the purpose of python-dotenv.

I am kind of confused with the documentation.

解决方案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.

Assuming you have created the .env file along-side your settings module.

.

├── .env

└── settings.py

Add the following code to your 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.

SOME_VAR=someval

# I am a comment and that is OK

FOO="BAR"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值