导入Python3的mechanize分支时出现错误
我已经为Python3安装了mechanize库。
但是,当我尝试导入它时,出现了这个错误。
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库已经安装在同一个虚拟环境目录里。
$ 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 个回答
1
https://github.com/adevore/mechanize/tree/python3
这个分支里根本没有 _sgmllib_copy.py 这个文件。我是从主分支拿的这个文件(需要把 print smth
改成 print (smth)
)。但是我还是不太明白怎么使用 import。_html.py 这个模块(在 mechanize 文件夹里)用的是
from . import _sgmllib_copy as sgmllib
这样写是错的吗?不过 from . import _beautifulsoup
似乎是可以正常工作的。
2
你提到的这个git仓库在使用import
的时候搞错了。mechanize._html
模块试图导入_sgmllib_copy
,它其实是想获取mechanize._sgmllib_copy
。不过,这种导入方式在PEP 328中已经不推荐使用了。正确的做法应该是使用相对导入,比如说from . import _sgmllib_copy
。