Category: Python
Environment variables in a Python application can be loaded from a .env
file using the dotenv
package.
Add the package:
pip install python-dotenv
Assume you have a .env
file in the root directory of your project:
export DOMAIN="someserver.com"
export SMTP_HOST="mail.${DOMAIN}"
export BATCH_INTERVAL="2"
Load environment variables as follows:
from dotenv import load_dotenv
# Load .env file
load_dotenv()
# Get values from .env file
smtp_host = os.getenv("SMTP_HOST")
batch interval = os.getenv("BATCH INTERVAL")
Note: Add .env to your .gitignore
file to ensure the file is not added to version control.
See python-dotenv for more details.