urllib "module object is not callable"
Asked Answered
N

3

8

This is my third python project, and I've received an error message: 'module object' is not callable.

I know that this means I'm referencing a variable or function incorrectly. But trial and error hasn't been able to help me solve this.

import urllib

def get_url(url):
    '''get_url accepts a URL string and return the server response code, response headers, and contents of the file'''
    req_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13',
        'Referer': 'http://python.org'}

    #errors here on next line
    request = urllib.request(url, headers=req_headers) # create a request object for the URL
    opener = urllib.build_opener() # create an opener object
    response = opener.open(request) # open a connection and receive the http response headers + contents

    code = response.code
    headers = response.headers # headers object
    contents = response.read() # contents of the URL (HTML, javascript, css, img, etc.)
    return code , headers, contents


testURL = get_url('http://www.urlhere.filename.zip')
print ("outputs: %s" % (testURL,))

I've been using this link for reference: http://docs.python.org/release/3.0.1/library/urllib.request.html

Traceback:

Traceback (most recent call last):
  File "C:\Project\LinkCrawl\LinkCrawl.py", line 31, in <module>
    testURL = get_url('http://www.urlhere.filename.zip')
  File "C:\Project\LinkCrawl\LinkCrawl.py", line 21, in get_url
    request = urllib.request(url, headers=req_headers) # create a request object for the URL
TypeError: 'module' object is not callable
Necolenecro answered 7/10, 2012 at 19:38 Comment(2)
Please include the traceback when you encounter a python error. Paste it, and indent it like code using the {} button on the editor toolbar.Biel
Thanks @MartijnPieters I updated me question with the traceback.Necolenecro
B
21

In python 3, the urllib.request object is a module. You need to call objects contained in this module. This is an important change from Python 2, if you are using example code you need to take that into account.

For example, creating the Request object and the opener:

request = urllib.request.Request(url, headers=req_headers)
opener = urllib.request.build_opener()
response = opener.open(request)

Read the documentation carefully.

Biel answered 7/10, 2012 at 19:41 Comment(3)
Thank you, my first two projects were in python 2.x last year. I was using a modified version of sample code that was referencing urllib2 which I read was now rolled up into urllib. Now that i'm aware of this difference i'll pay closer attention.Necolenecro
@Gimp: Several modules were merged and / or split into new namespaces. The urllib2 in python 2 was split across urllib.error and urllib.request. You may also want to glance through the What's new in Python 3.x series of documents.Biel
In Python 2 I had to use parse() method from urlparse module. However in Python 3 you have to from urllib import parseand then use parse.urlparse(). It's great. Thank you.Labia
S
6

urllib.request is a module. urllib.request.Request is a class. Calling a module like you're currently doing raises an error. You probably want to call the class, like this:

request = urllib.request.Request(url, headers=req_headers)  # create a request object for the URL

You'll also probably want to use build_opener of urllib.request rather than just urllib:

opener = urllib.request.build_opener()  # create an opener object
Smilacaceous answered 7/10, 2012 at 19:41 Comment(1)
Thank you for taking the time to answer my question icktoofay. I gave you a +1. @Martijn's edits were a bit faster and helped me solve my troubles first, whie providing the insight on the difference between python 2.x and 3.x It seems you both gave the same answers though. Thank you again for your time and effort.Necolenecro
M
0

It also occurs if you have declared the returning method as a property method by annotating with @property.

Memory answered 25/1, 2019 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.