python-dotenv is a ridiculously helpful Python library to programatically load environment variables you have in a .env
file into your script and Jupyter notebooks.
Install it:
pip install python-dotenv
Put this code block at the top of scripts or notebooks:
from dotenv import load_dotenv
# find .env automagically by walking up directories until it's found
dotenv_path = find_dotenv()
# load up the entries as environment variables
load_dotenv(dotenv_path)
If you want to load .env into your shell environment programmatically, you can run:
set -a; source .env; set +a
This is helpful when a Streamlit app or FastAPI application needs .env variables.
If you want zsh to do this automatically every time a new shell session is opened, you can use this plugin.