UnboundLocalError: local variable 'KeyError' referenced before assignment

798 views
Skip to first unread message

Casey Greene

unread,
Apr 9, 2012, 11:09:00 AM4/9/12
to django...@googlegroups.com
I have this very baffling error that I'm dealing with at the moment.
I'm maintaining someone else's code and I'm getting this error for
some queries with a try, except block in a view. It seems that the
queries that lead to the error are ones where there is, in fact, a
KeyError. I've actually manged to narrow it down to a pretty simple
test case though. Within the view, I can do this (first few lines):

def gene_view(request, slug=None, gene=None, org=None, template=None):
print(KeyError)

and I get this exception with django 1.4 (if it matters):
UnboundLocalError at /predictions/gene/
local variable 'KeyError' referenced before assignment
<etc>


In ./manage.py shell though, I can do:
./manage.py shell
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> print(KeyError)
<type 'exceptions.KeyError'>
>>> import urls
>>> print(KeyError)
<type 'exceptions.KeyError'>
>>> import predictions.views
>>> print(KeyError)
<type 'exceptions.KeyError'>
>>> import predictions.urls
>>> print(KeyError)
<type 'exceptions.KeyError'>

I was expecting that if there was some redefinition of KeyError that
one of those would also give the exception. I've also tried this,
just for kicks:

>>> from urls import *
>>> print(KeyError)
<type 'exceptions.KeyError'>
>>> from predictions.views import *
>>> print(KeyError)
<type 'exceptions.KeyError'>
>>> from predictions.urls import *
>>> print(KeyError)
<type 'exceptions.KeyError'>
>>>

I'm not really sure where to start tracking this down. I'm under the
impression that KeyError is one of the built in python exceptions, and
so it should really never be unbound.

Thanks for your help!
Casey

Karen Tracey

unread,
Apr 9, 2012, 11:20:30 AM4/9/12
to django...@googlegroups.com
On Mon, Apr 9, 2012 at 11:09 AM, Casey Greene <csgr...@princeton.edu> wrote:
> I have this very baffling error that I'm dealing with at the moment.
> I'm maintaining someone else's code and I'm getting this error for
> some queries with a try, except block in a view.  It seems that the
> queries that lead to the error are ones where there is, in fact, a
> KeyError.  I've actually manged to narrow it down to a pretty simple
> test case though.  Within the view, I can do this (first few lines):
>
> def gene_view(request, slug=None, gene=None, org=None, template=None):
>   print(KeyError)
>
> and I get this exception with django 1.4 (if it matters):
> UnboundLocalError at /predictions/gene/
> local variable 'KeyError' referenced before assignment

Python thinks KeyError is a local variable, thus it sounds like code
in the view subsequent to where KeyError is caught makes an assignment
to KeyError. So search later in the view for where KeyError is
mis-assigned.

Karen
--
http://tracey.org/kmt/

Casey Greene

unread,
Apr 9, 2012, 11:25:14 AM4/9/12
to django...@googlegroups.com
Great point Karen. It hadn't occurred to me that it could be a later
occurrence that could lead to python thinking it was local. Later:
except ValueError, KeyError

After changing it to:
except (ValueError, KeyError)
the issue is resolved.

Thanks so much!
Casey

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Reply all
Reply to author
Forward
0 new messages