Search code examples
pythonattributesmechanize

mechanize browser has no attribute


I've re written this twice, I can't see the problem,i've got mechanize installed on python 2 and I'm not doing both tabs and spaces, i'm only doing spaces.

import urllib
from bs4 import BeautifulSoup
import re
import urlparse
import mechanize

url = "http://www.dailymail.co.uk/home/index.html"
br = mechanize.Browser()
br.open(url)

for link in br.links():
    print link

and the error is

Traceback (most recent call last):
  File "mechanize.py", line 4, in <module>
    import mechanize
  File "/home/ro/Autoblog/mechanize/mechanize.py", line 8, in <module>
    br = mechanize.Browser()
AttributeError: 'module' object has no attribute 'Browser'

Solution

  • The filename of your Python file is "mechanize.py" (/home/ro/Autoblog/mechanize/mechanize.py). This shadows the name of the "mechanize" module. Because of this namespace collision, you are actually importing your own script when you do: import mechanize.

    Change the name of your "mechanize.py" script to something else and you should be good.