Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

export BUILDING_ADMINS='{
    "+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.

like image 926
dewet Avatar asked Sep 13 '14 18:09

dewet


People also ask

Can a dictionary value be a variable?

Variables can't be dict values. Dict values are always objects, not variables; your numbers dict's values are whatever objects __first , __second , __third , and __fourth referred to at the time the dict was created. The values will never update on the dictionary unless you do it manually. when they change..

Can a Python dictionary key be a variable?

An Overview of Keys and Values in Dictionaries in PythonKeys inside a Python dictionary can only be of a type that is immutable.

What is .ENV file in Python?

A . env file is a text file containing key value pairs of all the environment variables required by your application. This file is included with your project locally but not saved to source control so that you aren't putting potentially sensitive information at risk.


1 Answers

There is a option named environs, unfortunately, the way of exporting is a little different from the one you want, but I will suggest you because can be an alternative for you or other people (I needed to do this another day).

https://pypi.org/project/environs/

pip install environs

You can export via terminal or even creating a .env file

export BUILDING_ADMINS=+27792955555=De Wet,+27722855555=Marysol, +27878085555=Blomerus

In the code:

from environs import Env

env = Env()
env.read_env() // to allow reading from .env file

building_admins = env.dict('BUILDING_ADMINS', subcast=str)

result:

print(building_admins)
{
    "+27792955555": "De Wet",
    "+27722855555": "Marysol",
    "+27878085555": "Blomerus",
}
like image 88
Thiago Valentim Avatar answered Sep 21 '22 14:09

Thiago Valentim