How do i define 'HTTPCookieProcessor' global name?

917 views Asked by At

I have this code:

 class API(object):

        def __init__(self):
            self.baseuri = "http://api.xxx.xxx"
            self.cj = cookielib.CookieJar()
            self.cp = urllib2.HTTPCookieProcessor(self.cj)
            self.opener = urllib2.build_opener(self.cp)

the error is: NameError: global name 'HTTPCookieProcessor' is not defined How come? P.S.:

 >>> a = api.API()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python27\lib\lib\xxxxx\api.py", line 9, in __init__
        self.cp = urllib2.HTTPCookieProcessor(self.cj)
    NameError: global name 'HTTPCookieProcessor' is not defined
1

There are 1 answers

4
Eli Stevens On BEST ANSWER

The answer is probably contained in the three or so lines that got cut from the top of your api.py file when you pasted the code in your question. If I had to guess, I'd say they looked something like:

import cookielib
import urllib2
# <blank line>

Though I wouldn't be surprised if the import urllib2 line was actually something different, which is causing the issue somehow. If that's the case, try replacing whatever you have with just import urllib2; otherwise can you post the full file?