How to share variables among python scripts
Question: I am having trouble making variables accessible among different python
scripts. I have three scripts: shared.py test1.py test2.py  
In `shared.py`, I define a dictionary `devices` and initialize it.

    
    
    devices = {}
    

In `test1.py`, I import `shared.py` and add some value into `devices`

    
    
    import shared
    shared.devices['item1': 'item1']
    

In `test2.py`, I import `shared.py` and try to print the updated `devices` in
`shared.py`

    
    
    import shared
    print shared.devices
    

Instead of printing the updated value, it gives me an empty dictionary. My
understanding is that when I imported `shared` in `test2.py`, codes in
`shared.py` are executed again, so it leads to the re-initialization of
`devices`

Is there a way that I can access the updated `devices` in `test2.py`? Thanks
in advance.

**UPDATE1:** I changed my shared.py test1.py and test2.py according to a
similar solution from
[link](http://stackoverflow.com/questions/13034496/using-global-variables-
between-files-in-python) and then added test3.py

    
    
    # shared.py
    def init():
        global devices
        devices = {}
    
    # test1.py
    import shared
    
    def add(key, value):
        shared.devices[key] = value
    
    # test2.py
    import shared
    import test1.py
    
    shared.init()
    test1.add('key1', 'value1')
    print shared.devices
    
    # test3.py
    import shared
    print shared.devices
    

I executed the scripts in the following order:  
python test1.py (fine)  
python test2.py (fine)  
python test3.py (which gives me an error:"AttributeError: 'module' object has
no attribute 'devices'")

**UPDATE2:** To make my question more realistic. I have a `login.py` which
takes a few arguments and tries to connect to a switch.  
For example:  
python login.py --ip 10.1.1.1 --username admin --password admin

    
    
    # login.py - pseudocode
    switch = Switch() #create an object of Router class
    switch.connect(ip, username, password) #take the arguments from the command line and try to connect to a router
    switch_dict[admin] = switch #add switch instance to a shared dictionary switch_dict, which will be used by over scripts as well.
    

After connecting to a switch, I then need to execute another script `vlan.py`
to create a vlan on that switch

    
    
    # vlan.py - pseudocode
    # first I need to get the switch that was created by admin, not other users
    my_switch = switch_dict['admin']
    # Then I can perform some configuration on vlan
    my_switch.createvlan()
    

The problem is how can I create and store a dictionary `switch_dict` that can
be shared and accessed by other scripts??


Answer: You need some form of shared storage. You can use (depending on what you are
doing):

  1. A queue or a pipe ([documentation](https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes)).

  2. Shared memory ([documentation](https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes)).

  3. A third, independent store like a file (you can use json or pickle to serialize the object), memcache, redis, sqlite or a database.

The point being that this is not trivial and you need to decide (based on the
application) which option works for you.

