繁体   English   中英

导入mechanize时我在python3中出错

[英]I get an error in python3 when importing mechanize

导入mechanize时我在python3中出错。

我刚刚在我的virtualenv中安装了机械化,其中安装了python3。

$ 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中编程。

感谢您的帮助!

更新

我已经为python3安装了mechanize 现在,我有另一个错误。

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

唉,mechanize不支持Python 3. http://wwwsearch.sourceforge.net/mechanize/faq.html

Python 2.4,2.5,2.6或2.7。 Python 3尚不支持。

您可以在https://github.com/jjlee/mechanize/issues/96上对此问题发表评论


更新:我编写了自己的自动化库MechanicalSoup。 它与Python 3兼容https://github.com/hickford/MechanicalSoup

MechanicalSoup模块在同一个库中合并了Mechanical和BeautifulSoup,可以在Python 2.6到3.4之间使用。

在命令提示符下安装MechanicalSoup:

pip install MechanicalSoup

然后在python中导入,

import mechanicalsoup # Don’t forget to import the new module

我想你可能已经安装了机械化的Python 2版本,而新的Python 3导入语义正在打破旧的Python 2代码。 快速搜索,似乎Python 3对机械化的支持仍然有点弱。 但是,我确实找到了一个Python 3分支

您可以尝试从我链接到的源代码手动安装。 下载源代码后,请更改目录,以便进入mechanize目录。 然后在终端中输入$python3 setup.py install

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM