Search code examples
pythonmechanizemechanize-python

unable to import browser from mechanize


I am using mechanize for the first time. If I type the line from mechanize import Browserin python shell (interpreter) it doesn't give any error but when run the same code as a part of a .py file it gives the following error:

Traceback (most recent call last):
  File "/home/namit/Codes/BS4/mechanize.py", line 1, in <module>
    import mechanize
  File "/home/namit/Codes/BS4/mechanize.py", line 4, in <module>
    mech = mechanize.Browser()
AttributeError: 'module' object has no attribute 'Browser'

Solution

  • Change your filename from mechanize.py. Python is importing your file as the module, instead of importing the mechanize library.