Python httplib2 AttributeError: 'builtin_function_or_method' object has no attribute 'new'

StackOverflow https://stackoverflow.com/questions/9558212

  •  06-12-2019
  •  | 
  •  

Question

I am trying to scrape text from wikipedia. Since httplib2 is already installed, I thought I would use it.

When I go through the simple retrieval from their basic examples, the first example gives me this error.

> import httplib2
> h = httplib2.Http(".cache")
> url = "http://code.google.com/p/httplib2/"
> h.request(url, "GET")

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/httplib2/__init__.py", line 978, in request
  cached_value = self.cache.get(cachekey)
 File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/httplib2/__init__.py", line 625, in get
  cacheFullPath = os.path.join(self.cache, self.safe(key))
 File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/site-packages/httplib2/__init__.py", line 189, in safename
  filemd5 = md5.new(filename).hexdigest()
AttributeError: 'builtin_function_or_method' object has no attribute 'new'

I'm running on Mac OS X, Python Version: 7.2-2 (64-bit), Enthought Distribution.

I'm thinking it might be an issue with a faulty installation, but then again httplib2 came installed with other packages that I've been using. Also, I could try to reinstall httplib2, but I'm hesitant to do that, thinking it might break other things that are currently working.

Was it helpful?

Solution

Because the md5 module was deprecated (see http://docs.python.org/library/md5.html), httplib2 has code that checks dynamically for the old md5.new function or the new hashlib.md5 function. It's near the top of the module, and in the version that I have it looks like this:

# remove depracated warning in python2.6
try:
    from hashlib import sha1 as _sha, md5 as _md5
except ImportError:
    import sha
    import md5
    _sha = sha.new
    _md5 = md5.new

My guess is that your version of httplib2 is either too old and does not have this code, or it's going wrong somehow.

If you do have the newest version (or for some reason are unable/unwilling to upgrade), you can probably fix it dynamically in your program, but I'm not going to continue in that line, in the hope that upgrading will fix it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top