Html5Lib安装在Python 3.5而不是2.7

问题描述:

我已经越来越讨厌这个错误:Html5Lib安装在Python 3.5而不是2.7

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 

我试图理解为什么回溯正在研究这些文件。而且,当我做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'] 

我安装html5lib用命令easy_install 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 

我关心使用python 3.5它时,我的sys.path是看着我的2.7 Python代码在2.7上运行的DD。另外,我的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,我得到这个丑陋消息:

enter image description here

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

+0

您具体是如何安装该模块?并且请注意,Traceback不会自己查看模块,它只是帮助查找异常的工具。它是Python本身在执行'import ' –

+0

时寻找必要的模块我使用“easy_install html5lib”安装了html5lib。当我运行该命令时,它说'将html5lib 0.999999999添加到easy-install.pth文件 使用/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages 处理html5lib的依赖关系。 – Matt

+0

检查模块是否安装的最简单方法是在解释器中尝试'import '。如果它什么都没说,模块就安装好了。如果有例外......你知道演习。也许你可以尝试这种检查方法? –

我得到一个不同的错误,但对于Python 2.7版安装html5lib,这是我的解决方案:

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

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