Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NameError: global name 'any' is not defined

Tags:

python

any

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?

like image 958
Darwin Tech Avatar asked Jan 27 '12 17:01

Darwin Tech


People also ask

How do I fix NameError is not defined in Python?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

How do you correct a NameError in Python?

To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.

What is global name not defined in Python?

NameError: global name '---' is not definedOther names are defined within the program (such as variables). If Python encounters a name that it doesn't recognize, you'll probably get this error. Some common causes of this error include: Forgetting to give a variable a value before using it in another statement.

What is a NameError?

What is a NameError? A NameError is raised when you try to use a variable or a function name that is not valid. In Python, code runs from top to bottom. This means that you cannot declare a variable after you try to use it in your code. Python would not know what you wanted the variable to do.


1 Answers

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
like image 52
Ned Batchelder Avatar answered Sep 25 '22 03:09

Ned Batchelder