Values from outside of your application
Connecting to a database
Determining the operating system
Setting which need to change
Sensitive data
Reading an environmentaal variable
import os
os_version = os.getenv('OS')
print(os_version)
Using dotenv
Store environmental variables in text file
# .env file
DATABASE = Sample_Connection_string
#app.py
from dotenv import load_dotenv
import os
load_dotenv()
database = os.getenv('DATABASE')
print(database)
插入一个中文理解(转载自知乎用户【大大的甜甜圈】):
PS: 用 dotenv
就不用在各个文件中引入配置文件,保护敏感信息,利于后期代码维护。