init_dotenv.py 1.26 KB
Newer Older
崔为之's avatar
崔为之 committed
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version     : Python 3.11.4
# @Software    : Sublime Text 4
# @Author      : StudentCWZ
# @Email       : StudentCWZ@outlook.com
# @Date        : 2023/11/21 10:51
# @File        : init_dotenv.py
# @Description :
"""

崔为之's avatar
崔为之 committed
13
# Import standard library modules
崔为之's avatar
崔为之 committed
14 15
import os

崔为之's avatar
崔为之 committed
16
# Import third-party library modules
崔为之's avatar
崔为之 committed
17 18 19 20 21
from dotenv import load_dotenv


def init_dotenv() -> None:
    """
崔为之's avatar
崔为之 committed
22
    Initialize the dotenv extension.
崔为之's avatar
崔为之 committed
23

崔为之's avatar
崔为之 committed
24 25 26 27 28 29 30 31 32
    This function locates the `.flaskenv` file at the root of the project
    and loads it into environment variables using the dotenv package.

    The `.flaskenv` file is a convenient place to put configuration variables
    that should be set early in the application's life cycle, such as
    settings for the Flask command-line interface.

    Returns:
        None
崔为之's avatar
崔为之 committed
33
    """
崔为之's avatar
崔为之 committed
34 35
    # Get the root path to the project by removing the 'extensions'
    # part from the current file's absolute path
崔为之's avatar
崔为之 committed
36
    root_path = os.path.abspath(os.path.dirname(__file__)).split('extensions')[0]
崔为之's avatar
崔为之 committed
37 38

    # Construct the path to the .flaskenv file
崔为之's avatar
崔为之 committed
39
    flask_env_path = os.path.join(root_path, '.flaskenv')
崔为之's avatar
崔为之 committed
40 41

    # If the .flaskenv file exists, load it into environment variables
崔为之's avatar
崔为之 committed
42 43
    if os.path.exists(flask_env_path):
        load_dotenv(flask_env_path)