What module to use in shelve script

I have a script I just found on stack for shelving variables, and I get an error that reads:

'Traceback (most recent call last):
  File "/Users/*confidentialname*/Documents/Shelving.py", line 11, in <module>
    my_shelf[key] = globals()[key]
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shelve.py", line 124, in __setitem__
    p.dump(value)
_pickle.PicklingError: Can't pickle <class 'module'>: attribute lookup module on builtins failed
>>> '

What do I do? This is the link I found the code on: How can I save all the variables in the current python session? Here's the code:

import shelve

T='Hiya'
val=[1,2,3]

filename='/tmp/shelve.out'
my_shelf = shelve.open('Shelvingthing','n') # 'n' for new

for key in dir():
    try:
        my_shelf[key] = globals()[key]
    except TypeError:
        #
        # __builtins__, my_shelf, and imported modules can not be shelved.
        #
        print('ERROR shelving: {0}'.format(key))
my_shelf.close()
#To restore:

my_shelf = shelve.open(Shelvingthing)
for key in my_shelf:
    globals()[key]=my_shelf[key]
my_shelf.close()

print(T)
# Hiya
print(val)
# [1, 2, 3]

Update: I modified the code to look like instructed and got this warning:

ERROR shelving: builtins: Can't pickle <class 'module'>: attribute lookup module on 
builtins failed ERROR shelving: my_shelf: can't pickle _dbm.dbm objects 

ERROR shelving: shelve: Can't pickle <class 'module'>: attribute lookup 
module on builtins failed Hiya [1, 2, 3] >>> 


ANSWERS:


The error occurs when the code tries to pickle the shelve global variable - that is the one created by the import shelve statement! Modules picklable. I believe this code was written for another version of Python - in there a TypeError was thrown; but now now unpicklable values will throw _pickle.PickleError, which is not a TypeError.

Actually perhaps you'd want to just ignore any exceptions from shelve:

for key in dir():
    try:
        my_shelf[key] = globals()[key]
    except Exception as ex:
        #
        # __builtins__, my_shelf, and imported modules can not be shelved.
        #
        print('ERROR shelving: {}: {}'.format(key, ex))


 MORE:


 ? Shortcut to import modules?
 ? Difference between Java Interfaces and Python Mixin?
 ? Where to put @Transactional? In interface specification or implementation?
 ? Indexable interface
 ? 'Remove middleman' IntelliJ refactoring on an empty interface
 ? Why do I get error C3851 (a universal-character-name cannot designate a character in the basic character set)?
 ? What is the difference between the two statements?
 ? Java - declaring from Interface type instead of Class
 ? How to set properties of same name but different types in inherited classes
 ? Don't understand the statement "individual inherited values are not possible with any shorthand property"