简体   繁体   English

如何定义“ HTTPCookieProcessor”全局名称?

[英]How do i define 'HTTPCookieProcessor' global name?

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? 错误是: NameError: global name 'HTTPCookieProcessor' is not defined怎么来的? PS: PS:

 >>> 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

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. 答案可能包含在将代码粘贴到问题中时从api.py文件顶部剪切的三行左右。 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. 虽然如果import urllib2行实际上有所不同,我不会感到惊讶,但这以某种方式引起了问题。 If that's the case, try replacing whatever you have with just import urllib2 ; 如果是这种情况,请尝试将所有内容替换为import urllib2 ; otherwise can you post the full file? 否则,您可以发布完整文件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM