19

I get an error in python3 when importing mechanize.

I've just installed mechanize into my virtualenv where python3 is installed.

$ which python3
/Users/myname/.virtualenvs/python3/bin/python3

$ pip freeze
mechanize==0.2.5

But, when I try to import mechanize in my python code, I get this error.

import mechanize

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-6b82e40e2c8e> in <module>()
----> 1 import mechanize

/Users/myname/.virtualenvs/python3/lib/python3.3/site-packages/mechanize/__init__.py in <module>()
    117 import sys
    118 
--> 119 from _version import __version__
    120 
    121 # high-level stateful browser-style interface

ImportError: No module named '_version'

Does anyone know how to fix this problem?

I'm new to python and I've been studying how to program in python these days.

Thanks for your help in advance!

update

I've installed mechanize for python3. Now, I have an another error.

In [1]: import mechanize
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-6b82e40e2c8e> in <module>()
----> 1 import mechanize

/Users/myname/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/__init__.py in <module>()
    120 
    121 # high-level stateful browser-style interface
--> 122 from ._mechanize import \
    123      Browser, History, \
    124      BrowserStateError, LinkNotFoundError, FormNotFoundError

/Users/myname/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/_mechanize.py in <module>()

/Users/myname/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/_html.py in <module>()

ImportError: cannot import name _sgmllib_copy

I checked what I've installed in my working virtualenv. I found some warnings.

$ pip freeze
cssselect==0.9.1
httplib2==0.8
ipython==1.1.0
lxml==3.2.4
## FIXME: could not find svn URL in dependency_links for this package:
mechanize==0.2.6.dev-20140305
pyquery==1.2.8
Warning: cannot find svn location for mechanize==0.2.6.dev-20140305
Charles
  • 50,943
  • 13
  • 104
  • 142
crzyonez777
  • 1,789
  • 4
  • 18
  • 27
  • That's odd, I tried installing right now and it worked... – A.J. Uppal Mar 05 '14 at 06:46
  • 1
    Are you absolutely sure you're installing the Python 3 version of the library? Python 3 changed the semantics for relative imports, this breaks a lot of Python 2 code. Also, did you try importing the module from the interactive interpreter? – rectangletangle Mar 05 '14 at 06:48
  • @rectangletangle I tried to import mechanize in ipython in terminal. – crzyonez777 Mar 05 '14 at 06:49
  • 1
    @rectangletangle oh, you mean if I installed mechanize that's for python3? I didn't check if the one I installed through pip command is for python3. – crzyonez777 Mar 05 '14 at 06:53
  • I have the same problem installing python3 branch of mechanize – taras May 05 '14 at 21:41

3 Answers3

15

Alas, mechanize doesn't support Python 3. http://wwwsearch.sourceforge.net/mechanize/faq.html

Python 2.4, 2.5, 2.6, or 2.7. Python 3 is not yet supported.

You might like to comment on the issue at https://github.com/jjlee/mechanize/issues/96


Update: I wrote my own automating library MechanicalSoup. It's Python 3 compatible https://github.com/hickford/MechanicalSoup

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
5

The module MechanicalSoup merged Mechanical and BeautifulSoup in the same Library and can be used since Python 2.6 through 3.4.

in command prompt install MechanicalSoup:

pip install MechanicalSoup

Then import in python,

import mechanicalsoup # Don’t forget to import the new module
Ashna Talati
  • 51
  • 1
  • 1
  • Typo? I guess you meant Mechanize (not _Mechanical_) and BeautifulSoup. But that's not correct: [MechanicalSoup](https://github.com/MechanicalSoup/MechanicalSoup) is built on Python [Requests](http://docs.python-requests.org/en/latest/) (for HTTP sessions) and [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/) (for document navigation) ... so it does not use Mechanize. I think they just named it like that to remark it does a similar work as Mechanize, plus the benefits provided by BeautifulSoup. But thanks a lot for letting me know about this new great tool ! – abu Oct 12 '22 at 11:50
2

I think you may have installed the Python 2 version of mechanize, and the new Python 3 import semantics are breaking the old Python 2 code. A quick search, and it seems like Python 3 support for mechanize is still a bit weak. However, I did find a Python 3 branch.

You could try manually installing from the source code I linked to. Once you've downloaded the source, change your directory so that you're in the mechanize directory. Then enter $python3 setup.py install in the terminal.

rectangletangle
  • 50,393
  • 94
  • 205
  • 275
  • 2
    Thank you for you help! I've just installed the one you link to. But, I got another error. I'm going to edit my post to add the information. – crzyonez777 Mar 05 '14 at 07:09