Skip to content

Latest commit

 

History

History
44 lines (29 loc) · 1.24 KB

python-sensitive-data.md

File metadata and controls

44 lines (29 loc) · 1.24 KB

Python & Sensitive Data

You've written a script to connect with an API. It uses your personal API key. Now you want to share the script, but not your key. Here's how to keep your sensitive data private while making it easy for others to use.

Install python-decouple

The advantage of python-decouple over other .env methods is the ability to 'cast' variables as the correct data type. I arrived at python-decouple because other .env methods conflict with Tkinter file dialogs (weird but true).

py -m pip install python-decouple

Create Files

At your project root create these files:

  • .gitignore
  • .configure
  • .env
  • .env.example

Add Secrets to .env

IMPORTANT: Add .env to .gitignore.

SECRET_STRING = 'lsiuidu457sd2768_gh4879`
SECRET_FLOAT = 42.57
SECRET_BOOL = True

Create Globals in .configure

from decouple import config

TEST_STR = config('SECRET_STRING')
TEST_INT = config('SECRET_FLOAT', cast=float)
TEST_BOOL = config('SECRET_BOOL', default=False, cast=bool)

Supply a .env.example

This is a copy of .env with sensitive values removed. It gets distributed with the project. To use, rename to .env and enter your own information.