导入 mechanize 时在 python3 中出现错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22190503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:28:18  来源:igfitidea点击:

I get an error in python3 when importing mechanize

pythonpython-3.xmechanize

提问by crzyonez777

I get an error in python3 when importing mechanize.

导入 mechanize 时,在 python3 中出现错误。

I've just installed mechanize into my virtualenv where python3 is installed.

我刚刚将机械化安装到安装了 python3 的 virtualenv 中。

$ which python3
/Users/myname/.virtualenvs/python3/bin/python3

$ pip freeze
mechanize==0.2.5

But, when I try to import mechanize in my python code, I get this error.

但是,当我尝试在我的 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'

Does anyone know how to fix this problem?

有谁知道如何解决这个问题?

I'm new to python and I've been studying how to program in python these days.

我是 python 的新手,最近我一直在研究如何在 python 中编程。

Thanks for your help in advance!

提前感谢您的帮助!

update

更新

I've installed mechanize for python3. Now, I have an another error.

我已经为 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

I checked what I've installed in my working virtualenv. I found some warnings.

我检查了我在工作 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

回答by rectangletangle

I think you may have installed the Python 2 version of mechanize, and the new Python 3 import semantics are breaking the old Python 2 code. A quick search, and it seems like Python 3 support for mechanize is still a bit weak. However, I did find a Python 3 branch.

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

You could try manually installing from the source code I linked to. Once you've downloaded the source, change your directory so that you're in the mechanize directory. Then enter $python3 setup.py installin the terminal.

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

回答by Colonel Panic

Alas, mechanize doesn't support Python 3. http://wwwsearch.sourceforge.net/mechanize/faq.html

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

Python 2.4, 2.5, 2.6, or 2.7. Python 3 is not yet supported.

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

You might like to comment on the issue at https://github.com/jjlee/mechanize/issues/96

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



Update: I wrote my own automating library MechanicalSoup. It's Python 3 compatible https://github.com/hickford/MechanicalSoup

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

回答by Ashna Talati

The module MechanicalSoup merged Mechanical and BeautifulSoup in the same Library and can be used since Python 2.6 through 3.4.

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

in command prompt install MechanicalSoup:

在命令提示符下安装 MechanicalSoup:

pip install MechanicalSoup

Then import in python,

然后在python中导入,

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