【问题标题】:Html5Lib Installed in Python 3.5 but not 2.7Html5Lib 安装在 Python 3.5 但不是 2.7
【发布时间】:2017-04-27 04:23:03
【问题描述】:

我遇到了这个讨厌的错误:

Traceback (most recent call last):
  File "scrapeRecipe.py", line 29, in <module>
    br.select_form(name="aspnetForm")
  File "build/bdist.macosx-10.11-intel/egg/mechanize/_mechanize.py", line 619, in select_form
  File "build/bdist.macosx-10.11-intel/egg/mechanize/_html.py", line 260, in global_form
  File "build/bdist.macosx-10.11-intel/egg/mechanize/_html.py", line 267, in forms
  File "build/bdist.macosx-10.11-intel/egg/mechanize/_html.py", line 282, in _get_forms
  File "build/bdist.macosx-10.11-intel/egg/mechanize/_html.py", line 247, in root
  File "build/bdist.macosx-10.11-intel/egg/mechanize/_html.py", line 145, in content_parser
ImportError: No module named html5lib

我试图了解 Traceback 为何查看这些文件。此外,当我做locate build/bdist.macosx-10.11-intel/egg/mechanize/_mechanize.py 时,它告诉我它不存在。

我假设我收到此模块未找到错误,因为 html5lib 模块位于 /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/html5lib

问题:我们可以控制回溯的位置吗?此外,我的 sys.path 目前是

['/Users/madelinezechar/eatLowCarbon', '/Library/Python/2.7/site-
packages/BeautifulSoup-3.2.1-py2.7.egg', '/Library/Python/2.7/site-
packages/html2text-2016.9.19-py2.7.egg', '/Library/Python/2.7/site-
packages/mechanize-0.3.1-py2.7.egg', '/Library/Python/2.7/site-
packages/requests-2.13.0-py2.7.egg', '/Library/Python/2.7/site-
packages/pip-9.0.1-py2.7.egg', 
    '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',    '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',    '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',     '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',     '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',     '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',   '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',     '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', 
'/Users/madelinezechar/Library/Python/2.7/lib/python/site-packages',     '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', 
'/Library/Python/2.7/site-packages']

我使用命令easy_install html5lib 安装了html5lib。当我运行它时,我收到以下消息:

Adding html5lib 0.999999999 to easy-install.pth file

Using /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
Processing dependencies for html5lib
Finished processing dependencies for html5lib

当我的 sys.path 正在查看 2.7 并且我的 python 代码在 2.7 上运行时,我担心它使用 python 3.5。此外,我的 easy_install.pth 似乎没有 html5lib,即使在安装之后:

import sys; sys.__plen = len(sys.path)

 ./BeautifulSoup-3.2.1-py2.7.egg
   ./html2text-2016.9.19-py2.7.egg
   ./mechanize-0.3.1-py2.7.egg
   ./requests-2.13.0-py2.7.egg
   ./pip-9.0.1-py2.7.egg
   import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

当我运行 pip3 freeze 时,我看到列出了 html5lib。当我运行pip freeze 时,我没有。如果我尝试sudo pip install html5lib,我会收到这条丑陋的消息:

这个错误信息是什么意思?当我知道html5lib.pyPython/2.7/site-packages/bs4/builder 中时,pip freeze 怎么可能不返回html5lib

【问题讨论】:

  • 您是如何安装该模块的?请注意,Traceback 并不查看模块本身,它只是帮助定位异常的工具。在执行import &lt;module&gt; 时,是 Python 本身在寻找必要的模块
  • 我使用“easy_install html5lib”安装了 html5lib。当我运行该命令时,它显示Adding html5lib 0.999999999 to easy-install.pth file Using /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages Processing dependencies for html5lib
  • 检查模块是否安装的最简单方法是在解释器中尝试import &lt;module&gt;。如果它什么也没说,则模块已安装。如果有例外......你知道演习。也许你可以试试这种检查方法?
  • 没有例外,但我也一样,ImportError: No module named html5lib
  • 这是一个例外 :) 这意味着该模块仍然没有安装。不能用pip安装吗?

标签: python python-2.7 sys traceback


【解决方案1】:

我得到一个不同的错误,但要为 python 2.7 安装 html5lib,这是我的解决方案:

pip install --ignore-installed six --user
sudo -H pip install html5lib --ignore-installed

要了解更多信息,这是一个很棒的帖子:https://github.com/pypa/pip/issues/3165

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2017-05-01
    • 2015-12-25
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    相关资源
    最近更新 更多