Natasha
Natasha

Reputation: 103

Setting up environmental variables in Python

Long time creeper, first time caller. I was trying to create and use environ var to send gmail (smtp) from a settings.py file, but clearly I was doing it wrong because when I put in my password it worked, but when I used os.environ.get to hide the password.

I think I didn't call the environmental variable correctly but I have no clue! I got an authentication error

import os

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'Thisworkedfine'

But when I used this it didn't work after defining 'EMAIL_USER' and 'EMAIL_PASS' in my System Properties.


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')

Error from hell:

SMTPSenderRefused at /password-reset/
(530, b'5.5.1 Authentication Required. Learn more at\n5.5.1  https://support.google.com/mail/?p=WantAuthError c2sm2597974pjs.13 - gsmtp', 'webmaster@localhost')

HALP!

Upvotes: 2

Views: 6886

Answers (3)

LoudEye
LoudEye

Reputation: 37

In another file (something like envvar.py) create two variables. One for email and one for password:

email = "[email protected]"
password = "somepassword"

then in your settings file:

import envvar

EMAIL_HOST_USER = envvar.email
EMAIL_HOST_PASSWORD = envvar.password

Then add envvar to your gitignore

Upvotes: 0

Omar
Omar

Reputation: 218

I highly recommend using dotenv module for python, I personally found this to be the most hassle free way to deal with environment variables.

Chuck all your evnironment variables in a .env file which is just key value pairs. So the content of your .env file would look something like this:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'Thisworkedfine'

Then you in whichever python file you want to use your envars you can do this:

import os
from dotenv import load_dotenv

EMAIL_BACKEND = os.getenv('EMAIL_BACKEND')

Then just make sure to include your .env in your .gitignore so that your secrets always stay local.

for more info check out the dotenv github repo here

Upvotes: 2

Ashish Kumar
Ashish Kumar

Reputation: 593

One way is to set your variables in another python file and import the file.

Create a file say,myEnvVal.py

import os
# Set environment variables
def setVar():
    os.environ['EMAIL_USER'] = '[email protected]'
    os.environ['EMAIL_PASSWORD'] = 'Thisworkedfine'

Now import this file

import os
import myEnvVal
myEnvVal.setVar()
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')

Upvotes: 3

Related Questions