How to store a Python dictionary as an environment variable

I am trying to save an environment variable that Python can read as a dictionary. If this is a bad idea, please let me know your suggestions, I really want to study. I worry that if I store data as many environment variables, it can become difficult to handle.

I programmed Raspberry Pi to unlock a door based on caller ID of incoming phone calls to Twilio number, it works fine. I want to share my code on Github for feedback, but I don't want to share the list of phone numbers with the world, so I'm trying to save it as an environment variable, and then let Python read it.

Phone numbers are in the Python dictionary like this.

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

I try to save it as an environment variable, as on Ubuntu 14.04

 export BUILDING_ADMINS='{ "+27792955555": "De Wet", "+27722855555": "Marysol", "+27878085555": "Blomerus", }' 

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

2) I feel that there should be a better way to do this, and I am going in the wrong direction, but nothing that I seem to have found on Google is a solution to the problem I am facing. Please point me in the right direction.

+5
source share
4 answers

An environment variable is not something that the user would like to give the user script. Use json module and file:

 import json with open('numbers') as f: numbers = json.load(f) print numbers['+27792955555'] # De Wet 

When uploading to github, do not upload the file with numbers or do not upload a fake file.

+2
source

If you decide to use the environment, you should serialize the Python dictionary as JSON and dump / load, which is when setting up / getting a variable environment. You can access the environment using the os module environ attribute. You can reset / load JSON using json module. You might want to keep track of the maximum length in environment variables if there is such a thing.

If in your place I would use a SQLite database, see https://docs.python.org/2/library/sqlite3.html . This will provide you with consistency, a specific scheme, and a good interface for processing your data.

+3
source

I do not know if this is what you were looking for, but I ended up here trying to save the dictionary as a Linux environment variable to consume it in my application.

What I did was save it as a string like this:

 export BUILDING_ADMINS="{'+27792955555': 'De Wet','+27722855555': 'Marysol','+27878085555': 'Blomerus'}' 

And then on the python code you read it and turn it into a dictionary using this (taken from: Convert a string representation of a dictionary to a dictionary ?)

 import ast import os ba_dict = ast.literal_eval(os.environ["BUILDING_ADMINS"]) 

If you type

 type(ba_dict) 

You should see that your line is now a dict.

 <class 'dict'> 

Hope this helps someone else!

+1
source

It is possible! Just do

 import os os.environ.__dict__["_data"]["yourkey"] = {"key":0} #Your data print (os.environ.__dict__["_data"]["yourkey"]) #Will print out value! 

It is a bit complicated, but it works!

-1
source

Source: https://habr.com/ru/post/1202507/


All Articles