Darwin Tech Darwin Tech - 7 months ago 87
Python Question

Python NameError: global name 'any' is not defined

I am getting the following error on my production server:

Traceback (most recent call last):

File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
File "myproject/middleware.py", line 31, in process_request
if not any(m.match(path) for m in EXEMPT_URLS):

NameError: global name 'any' is not defined


The server is running python 2.6 and in development this error was not raised. The offending code is in
middleware.py
:

...
if not request.user.is_authenticated():
path = request.path_info.lstrip('/')
if not any(m.match(path) for m in EXEMPT_URLS):
return HttpResponseRedirect(settings.LOGIN_URL)


Should I rewrite this
any
function to work around the problem?

Answer

You are actually running on Python 2.4, which doesn't have an any builtin.

If you need to define your own any, it's easy:

try:
    any
except NameError:
    def any(s):
        for v in s:
            if v:
                return True
        return False
Comments