【问题标题】:Getting an error when importing mechanize branch for python3为 python3 导入机械化分支时出错
【发布时间】:2014-03-19 04:19:17
【问题描述】:

我已经为 python3 安装了机械化库。 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

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

谁能帮我解决这个问题?

提前谢谢你!

【问题讨论】:

  • 你有没有在 Python 3 上工作过 mechanize?

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


【解决方案1】:

您提到的 git 存储库使用 import 错误。 mechanize._html 模块 imports _sgmllib_copy 期望得到 mechanize._sgmllib_copy,但这种导入方式在 PEP 328 中已被弃用。相反,它应该使用相对进口,例如from . import _sgmllib_copy.

【讨论】:

  • 非常感谢您的帮助!那么,我需要做的是修复“mechanize._html”文件?我没有看到那个 html 文件。
  • 支持mechanize._html模块的文件当然是mechanize/_html.py
【解决方案2】:

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 似乎工作正常。

【讨论】:

  • 感谢您的帮助!!我从 master 分支添加了 _sqmllib_copy 并重新安装了它。现在我有这个错误“IndentationError:意外缩进”......
  • 所以你可以在源文件中修复这个错误。它是否显示它发生的行号?或者你可以尝试使用我的修复drive.google.com/file/d/0B2tqHfR7_fJua2pwNUFmSDlIWnM/… 它安装但仍然不适合我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多