dewet dewet - 1 year ago 65
Python Question

How to store a Python dictionary as an Environment Variable

I am trying to store an Environment Variable that Python can read in as a Dictionary. If this is a bad idea, please let me know your suggestions, I am eager to learn. I'm worried that if I store the data as many environment variables it might become hard to manage.

I programmed a Raspberry Pi to unlock a door based off the Caller ID's of incoming phone calls to a Twilio number, it is working great. I want to share my code on Github for feedback but I do not want to share my list of phone numbers with the world so I am trying to save it as an environment variable and then let Python read it in.

The phone numbers are in a Python Dictionary like this.

building_admins = {
"+27792955555": "De Wet",
"+27722855555": "Marysol",
"+27878085555": "Blomerus",

I am trying to save it as an Environment Variable like this on Ubuntu 14.04

"+27792955555": "De Wet",
"+27722855555": "Marysol",
"+27878085555": "Blomerus",

1) I can't get Linux to save the environment variable, is there something I could do to make it work?

2) I feel like there must be a better way to do this and I am going in the wrong direction but nothing I seem to find with Googling is solving the problem I am facing. Please point me in the right direction.

Answer Source

An environment variable is not somwthing a script user would like to set. Use the json module and a file:

import json

with open('numbers') as f:
    numbers = json.load(f)

print numbers['+27792955555']    #  De Wet

When uploading to github don't upload the numbers file or upload a fake one.