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.
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..
An Overview of Keys and Values in Dictionaries in PythonKeys inside a Python dictionary can only be of a type that is immutable.
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.
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",
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With