【发布时间】:2014-04-07 01:59:33
【问题描述】:
我在导入 mechanize 时遇到 python3 错误。
我刚刚在安装了 python3 的 virtualenv 中安装了 mechanize。
$ which python3
/Users/myname/.virtualenvs/python3/bin/python3
$ pip freeze
mechanize==0.2.5
但是,当我尝试在我的 python 代码中导入 mechanize 时,我得到了这个错误。
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'
有人知道如何解决这个问题吗?
我是python新手,这几天一直在研究如何用python编程。
提前感谢您的帮助!
更新
我已经安装了mechanize for python3。 现在,我又遇到了一个错误。
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
我检查了我在工作的 virtualenv 中安装的内容。 我发现了一些警告。
$ 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
【问题讨论】:
-
这很奇怪,我现在尝试安装并且成功了...
-
您确定要安装 Python 3 版本的库吗? Python 3 改变了相对导入的语义,这破坏了很多 Python 2 代码。另外,您是否尝试过从交互式解释器中导入模块?
-
@rectangletangle 我尝试在终端的 ipython 中导入 mechanize。
-
@rectangletangle 哦,你的意思是如果我安装了用于 python3 的 mechanize?我没有检查我通过pip命令安装的那个是不是python3的。
-
我在安装 mechanize 的 python3 分支时遇到同样的问题
标签: python python-3.x mechanize