ImportError: lxml not found, please install it

ImportError: lxml not found, please install it

我正在通过 udemy 上的 jupyter notebook 工作,这个主题是关于 pandas,关于其中一个问题我收到此错误消息:

ImportError                               Traceback (most recent call last)
<ipython-input-6-dc8b64eb545d> in <module>()
----> 1 df = pd.read_html('http://www.fdic.gov/bank/individual/failed/banklist.html')

c:\users\phillip.desktop-did4ci8\appdata\local\programs\python\python37-32\lib\site-packages\pandas\io\html.py in read_html(io, match, flavor, header, index_col, skiprows, attrs, parse_dates, tupleize_cols, thousands, encoding, decimal, converters, na_values, keep_default_na, displayed_only)
    985                   decimal=decimal, converters=converters, na_values=na_values,
    986                   keep_default_na=keep_default_na,
--> 987                   displayed_only=displayed_only)

c:\users\phillip.desktop-did4ci8\appdata\local\programs\python\python37-32\lib\site-packages\pandas\io\html.py in _parse(flavor, io, match, attrs, encoding, displayed_only, **kwargs)
    791     retained = None
    792     for flav in flavor:
--> 793         parser = _parser_dispatch(flav)
    794         p = parser(io, compiled_match, attrs, encoding, displayed_only)
    795 

c:\users\phillip.desktop-did4ci8\appdata\local\programs\python\python37-32\lib\site-packages\pandas\io\html.py in _parser_dispatch(flavor)
    749     else:
    750         if not _HAS_LXML:
--> 751             raise ImportError("lxml not found, please install it")
    752     return _valid_parsers[flavor]
    753 

ImportError: lxml not found, please install it

(我用的是python3.7,我已经下载了anaconda)

然后我尝试使用以下命令在命令提示符下安装 lxml:

pip3 install lxml

但是我收到此错误消息:

Command "c:\users\phillip.desktop-did4ci8\appdata\local\programs\python\python37-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\Users\PHILLI~1.DES\AppData\Local\Temp\pip-install-i762jowc\lxml\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\PHILLI~1.DES\AppData\Local\Temp\pip-record-z7kvhhca\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\PHILLI~1.DES\AppData\Local\Temp\pip-install-i762jowc\lxml\

我不确定如何解决这个问题。

从那里打开 Anaconda Navigator,单击环境 select 您当前正在使用的环境,然后从那里安装包。原因是你的 jupyter notebook 没有使用默认的 python 安装。并且在不切换到正确环境的情况下仅在终端中使用 pip3 命令是行不通的。另外,我在从 Anaconda 终端安装时遇到了一些问题。

编辑

从导航器安装还将安装所有需要的依赖项,包括 C 库。这确实是在您的 jupyter/Anaconda 项目中管理包的最简单方法。

EDIT2

根据您的评论,我感觉您的 jupyter notebook 使用了不同的 python 环境。 Anaconda 附带 python 3.6 而不是 3.7。加上你错误中的路径:

Command "c:\users\phillip.desktop-did4ci8\appdata\local\programs\python\python37-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\Users\PHILLI~1.DES\AppData\Local\Temp\pip-install-i762jowc\lxml\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\PHILLI~1.DES\AppData\Local\Temp\pip-record-z7kvhhca\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\PHILLI~1.DES\AppData\Local\Temp\pip-install-i762jowc\lxml\

建议使用不同于 Anaconda 基础(root)的环境。尝试从导航器中使用午餐 jupyter,这将确保您使用正确的环境

使用anaconda环境安装缺少的包

步骤:

  1. 转到 anaconda 控制台
  2. 确保切换到用于运行页面顶部“应用程序”右侧栏中脚本的环境。
  3. 单击左侧栏中的环境
  4. 键入包的名称 - “lxml” - 在本例中
  5. 将下拉按钮从“已安装”切换为“未安装”
  6. 包裹出现后,勾选复选框
  7. 点击屏幕底部的应用
  8. 安装后,返回您的页面并重新启动 jupyter notebook。

这应该可以解决问题。