SoFunction
Updated on 2024-11-17

Python libraries - dotenv package and .env configuration file details

python library-dotenv package | .env configuration files

contexts

We develop each system is inseparable from the configuration information, this information is very sensitive, once leaked out the consequences are very serious, the reason for being leaked is usually programmers will be mixed with the configuration information and code.

Sensitive information is loaded through environment variables in general business code.

Setting sensitive information as environment variables, but there is so much of such information that it is too much of a hassle to set them one by one. You must want to be able to put this sensitive information in a separate file, always managed separately from the code.

In python projects, sensitive information (such as database passwords) is more recommended to use the.envfiles to be managed separately and not included in the git management. Instead, the more popular parsing .env is python-dotenv.

python-dotenv can automatically add configuration information from configuration files to environment variables. python-dotenv solves the problem of separating code from sensitive information.

flask officially recommends using the python-dotenv package to manage special configurations.

How to use python-dotenv

Ideas for use: The simplest and most common usage is to call theload_dotenv, from the current directory or its parent directory in the.envfile or a specified path to load environment variables, then you can call theEnvironmentally relevant methods provided.

load_dotenv does not update already existing configuration entries by default. It is recommended to use the override parameter as follows:

from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(), override=True)
  • dotenv_path: Specifies the path of the .env file, of course, if you don't pass this parameter (the default is None), it will also call dotenv.find_dotenv() to find the location of the file, but if your filename is not .env, then you must pass this parameter.
  • override: When there is a conflict between a variable in the .env file and an environment variable in the system, the system variable will be used by default according to the above order of values, if you want to override the system variable with a variable in .env, you can pass the parameter override=True to load_dotenv(), which will only temporarily use the value of the variable in .env.
import os
from dotenv import find_dotenv, load_dotenv
# Load the .env file into the environment variable
load_dotenv(find_dotenv('.env'))
print(("URL"))

Once the file is loaded, you can read the contents by reading from the environment variable.

To this point this article on the python library-dotenv package | .env configuration file is introduced to this article, more related to python env configuration file content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!