本文介绍了ImportError:导入mechanize时没有名为'_version'的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过pip安装了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'

site-packages目录中存在 -version.py 文件:

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

我缺少什么?

推荐答案

如果你看看你会看到 mechanize Python 2.x package:

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

除此之外,您还可以在中看到mechanize / __ init __。py 所有进口都是相对的:

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

from _version import __version__

而非显式:

from ._version import __version__

为 Py3 支持,它列出了一些你可以尝试的替代方案。那,或者说它: - )。

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

这篇关于ImportError:导入mechanize时没有名为'_version'的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:38