1
  1. settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_USE_TLS = True

EMAIL_PORT = 587
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
  1. .env
export EMAIL_HOST_PASSWORD=<>
export EMAIL_HOST_USER = <>
export EMAIL_HOST=<>

3.Termial

(Carealenv) E:\atom data\mywebsite>source .env
'source' is not recognized as an internal or external command,
operable program or batch file.

I am having error of SMTPserverdisconnected .. please run connect first I want to connect my .env file to django so that SMTPServer get connected and i can send verfication email to users. It will be great help.. Thank you

NRA 1
  • 137
  • 1
  • 1
  • 10

2 Answers2

2

Further to ruddra's answer, you can alternatively use python-dotenv.

pip install python-dotenv

then in your settings.py

# settings.py
from dotenv import load_dotenv
load_dotenv()

Then you can use the getenv or as you have in your example environ.get function from the built-in os module to grab env variables and provide defaults if they do not exist.

from os import getenv

EMAIL_HOST = getenv('EMAIL_HOST', 'localhost')

Where localhost is whatever you wish to set as default.

Llanilek
  • 3,386
  • 5
  • 39
  • 65
1

You can't use source in Windows machine as described in the Stackoverflow answer.

If you want to use .env file, consider using libraries like django-environ which will read the file directly, no need to load it from ENVIRONMENT Variables.

Sample usage in settings.py:

import environ
env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()

# False if not in os.environ
EMAIL_HOST = env('EMAIL_HOST')

Another alternative solution is to use Docker with docker-compose, where you can load the environment variables from file.

ruddra
  • 50,746
  • 7
  • 78
  • 101