python-dotenv library

  1. Install python-dotenv library from the command line:
pip install python-dotenv
  1. Load environment variables from .env file:
import os
from os.path import dirname, abspath
from dotenv import load_dotenv

path = dirname(abspath(__file__)) + '/.env'

envVar = os.getenv('HOST')

print(envVar)  # localhost

Leave a Comment

Your email address will not be published. Required fields are marked *