导入python3的mechanize分支时出错

时间:2014-03-19 04:19:18

标签: python python-3.x mechanize-python

我已经为python3安装了mechanize库。 https://github.com/adevore/mechanize/tree/python3

但是,当我导入它时,我收到了这个错误。

Python 3.3.3 (default, Dec 30 2013, 16:15:14) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import mechanize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Username/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/__init__.py", line 122, in <module>
  File "/Users/Username/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/_mechanize.py", line 15, in <module>
  File "/Users/Username/.virtualenvs/python3/lib/python3.3/site-packages/mechanize-0.2.6.dev_20140305-py3.3.egg/mechanize/_html.py", line 16, in <module>
ImportError: cannot import name _sgmllib_copy

但是,我确信mechanize安装在同一个virtualenv目录中。

$ pip freeze
## 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

我不习惯在终端中操作,所以我不知道如何解决这个问题。

有人可以帮我解决这个问题吗?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

您引用的git存储库使用import错误。期待获得mechanize._html的{​​{1}}模块import s _sgmllib_copy,但PEP 328已弃用该导入方式。相反,它应该使用相对导入,例如mechanize._sgmllib_copy

答案 1 :(得分:1)

https://github.com/adevore/mechanize/tree/python3

这个分支根本不包含_sgmllib_copy.py。我从master分支获取此文件(需要将print smth更改为print (smth))。但我仍然不知道如何使用导入。在_html.py模块中(它位于mechanize文件夹中)使用

from . import _sgmllib_copy as sgmllib

这是错的吗?但from . import _beautifulsoup似乎正在发挥作用。