Ipython deep_reload Exception when importing from sqlalchemy
Question: I am working on several modules of my python library, doing most of the
testing through IPython.

Anytime I attempt to reload (deep_reload) a module that uses sqlalchemy, the
reload throws an Exception and fails to reload the module (I must start a new
kernel and re-import). More specifically, any module that imports my
sqlalchemy declarative models.

The traceback is quite long, so I will summarize for now, and provide more if
requested.

* * *

**Before the Exception, StdOUT reads (this is only the bottom portion):**

    
    
    Reloading pysqlite2
    Reloading sqlalchemy.dialects.sqlite.sqlite3
    Reloading sqlite3
    Reloading sqlite3.dbapi2
    Reloading sqlite3.datetime
    Reloading sqlite3.time
    Reloading sqlite3._sqlite3
    Reloading _sqlite3
    

**Then the exception is thrown, and here is the most recent entry of the
traceback:**

    
    
    \pydir\lib\site-packages\sqlalchemy-0.7.9-py2.7.egg\sqlalchemy\util\_collections.pyc in __setitem__(self, object, value)
        697         if oid not in self._weakrefs:
        698             self._weakrefs[oid] = self._ref(object)
    --> 699         weakref.WeakKeyDictionary.__setitem__(self, object, value)
        700 
        701     def __delitem__(self, object):
    
    TypeError: unbound method __setitem__() must be called with WeakKeyDictionary instance as first argument (got WeakIdentityMapping instance instead)
    

* * *

* * *

## Edit: Problem Update

When calling reload, I started adding exclusions based on the module on the
top of the traceback stack ('_sqlite3' in the example above).

What I found was that I kept getting the same Error, only it would come from a
new module ('pysqlite2', 'sqlite3'). And in each case, there was always
SqlAlchemy higher up on the traceback, more specifically,
'sqlalchemy.orm.mapper'.

When defining my model orm classes, I was defining my orm classes and adding
them to the session directly from the module on import. The solution was to
wrap these few lines in a function that should not be executed on import.
Seems to fix.

I think this was just a case of some lazy coding finally catching up with me.


Answer: You might add sqlite3 (or pysqlite2) to deep reload's exclusions:

    
    
    dreload(mymod, exclude=['sys', 'os.path', '__builtin__', '__main__', 'sqlite3'])
    

etc.

