https://github.com/theskumar/python-dotenv
You first create your .env
file and write your various variables. For example a .env
might look like:
password=SUPERSECRETPASSWORD
username=myusername
and so forth. Then you will load your .env
using the load_dotenv()
function.
This is an example from some code I have in a project:
import os
import psycopg2 as pg2
from dotenv import load_dotenv
# -------------------------------
# Connection variables
# -------------------------------
dotenv_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', '.env')) #travels up a level to find the .env
load_dotenv(dotenv_path)
# -------------------------------
# Connection to database
# -------------------------------
# Server connection
db_url = os.environ.get("DATABASE_URL")
CONN = pg2.connect(db_url, sslmode="require")
Please make sure you add .env
to your .gitignore
file!!!!
EDIT: NVM I misread the question. I'll leave it in though.