Search code examples
pythonpython-3.ximportmechanize

ImportError: No module named '_version' when importing mechanize


I installed mechanize via pip and get an errer when I import the module:

$ python
Python 3.5.2 (default, Jun 28 2016, 08:46:01) 
[GCC 6.1.1 20160602] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/site-packages/mechanize/__init__.py", line 119, in <module>
    from _version import __version__
ImportError: No module named '_version'

The file -version.py is present in the site-packages directory:

$ ls /usr/lib/python3.5/site-packages/mechanize
_auth.py               __init__.py           _response.py
_beautifulsoup.py      _lwpcookiejar.py      _rfc3986.py
_clientcookie.py       _markupbase.py        _sgmllib_copy.py
_debug.py              _mechanize.py         _sockettimeout.py
_firefox3cookiejar.py  _mozillacookiejar.py  _testcase.py
_form.py               _msiecookiejar.py     _urllib2_fork.py
_gzip.py               _opener.py            _urllib2.py
_headersutil.py        _pullparser.py        _useragent.py
_html.py               __pycache__           _util.py
_http.py               _request.py           _version.py

What am I missing?


Solution

  • If you look at setup.py you'll see mechanize is a Python 2.x package:

    Programming Language :: Python
    Programming Language :: Python :: 2
    Programming Language :: Python :: 2.4
    Programming Language :: Python :: 2.5
    Programming Language :: Python :: 2.6
    Programming Language :: Python :: 2.7
    

    Apart from that, you can see in mechanize/__init__.py that all imports are relative:

    from _version import __version__
    

    instead of explicit:

    from ._version import __version__
    

    In python 3, this results in import errors.

    There's an issue opened for Py3 support and it lists some alternatives you could try. That, or port it :-).