Python Quick Tip: Hiding Passwords and Secret Keys in Environment Variables (Mac & Linux)

Corey Schafer · 171K views · 2018 · 4:32

Open on YouTube

Python, Ruby & scripting languagesSecurity & hackingSoftware practice, open source & careerstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to hide passwords, email credentials or database URLs and need a short walkthrough of setting environment variables on Mac or Linux.

Recommended by 2 people across 2 different months, 2019 to 2020.

What it is

A 4.5 minute Python tutorial by Corey Schafer showing how to keep passwords and secret keys out of source code by storing them in environment variables on Mac and Linux. It covers setting variables in the shell profile and reading them in Python with os.environ. A companion video covers the Windows setup.

What people said

Recommends TobKed · 2019-07 · Stack Overflow · on “How to hide EMAIL_HOST_PASSWORD in settings.py file?”

Two things about your question: You cannot deploy Django app on github pages because they serve only static files. Check e.g. Heroku, it's quite easy to deploy Django there. The most common practice to hide passwords and secret keys is to use environmental variables. Generally, you have to set a variable in bash e.g. export EMAIL_HOST_PASSWORD=my-own-password and then you can use os python module to retrieve it: import os EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD") Useful video tutorials: Python Django Tutorial: Deploying Your Application (Option #2) - Deploy using Heroku [LINK] Python Quick Tip: Hiding Passwords and Secret Keys in Environment Variables (Mac & Linux) [this video]

Recommends aconite.io · 2020-05 · Stack Overflow · on “How to set DATABASE_URL environment variable for Postgres Mac terminal?”

Yes, you have to do that manually. So, within your terminal do this: % cd % nano .bash_profile It will open a file 'bash_profile' in editor in your terminal. And in there you can set environmental variables like: export DATABASE_URL="YOUR DATABASE URL" Check this video out for more info: [this video]

Cites requiem · 2019-06 · Stack Overflow · on “Error occuring while sending test mail using flask-mail”

You will want to use os.environ example: app.config['MAIL_USERNAME'] = os.environ.get('EMAIL_USER', 'my@email.com') app.config['MAIL_PASSWORD'] = os.environ.get('EMAIL_PASS', 'mypassword') or set environment variables windows: [LINK] linux: [this video]