Python: ImportError: lxml not found, please install it

Viewed 90881

I have the following code (in PyCharm (MacOS)):

import pandas as pd

fiddy_states = pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states')

print(fiddy_states)

And I get the following error:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/user_name/PycharmProjects/PandasTest/Doc3.py
Traceback (most recent call last):
  File "/Users/user_name/PycharmProjects/PandasTest/Doc3.py", line 9, in <module>
    fiddy_states = pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/html.py", line 906, in read_html
    keep_default_na=keep_default_na)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/html.py", line 733, in _parse
    parser = _parser_dispatch(flav)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/html.py", line 693, in _parser_dispatch
    raise ImportError("lxml not found, please install it")
ImportError: lxml not found, please install it

In Anaconda does appear installed the last version of lxml (3.8.0). Despite of that, I have tried to reinstall it by: 1) writing pip install lxml and 2) downloading the lxml wheel corresponding to my python version (lxml-3.8.0-cp36-cp36m-win_amd64.whl), but in any case all remains the same (in the second case I get that it is not a supported wheel on this platform, even though the version of python is correct (3.6, 64 bits)).

I've read similar questions here (even with the same code above, since it's from a tutorial), but the problem still persists.

10 Answers

For people reached here using Jupyter notebook, I restarted the kernel after pip install lxml and the error is gone.

I got same error, it seems that my python3 was pointing to pandas in python2 (since I have not install pandas in python3). After doing pip3 install pandas and restarting a notebook, it worked fine.

You can go to Settings > Project Interpreter > Click on '+' icon
Find 'lxml' from the list of packages and click 'Install Package' button found below.

I am using PyCharm 2019.2.1 (Community Edition)
Build #PC-192.6262.63, built on August 22, 2019
Runtime version: 11.0.3+12-b304.39 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.15.0-58-generic
GC: ParNew, ConcurrentMarkSweep
Memory: 937M
Cores: 4

  1. you may have to (re)install some of your libraries pip install lxml bs4 html5lib

  2. pd.read_html() reads with 'lxml' library by default, so try another library that you installed above like pd.read_html(some_url, flavor='html5lib')

I tried to reinstall lxml without any progress.

I ended uninstalling pandas and reinstalling and updating and that solved my issues!

pip uninstall pandas  
pip install pandas
pip3 install --upgrade pandas

I got the same error when trying to run some code that was using pandas. I tried some suggestions here but those did not work. Finally, what worked for me was the following two steps :

conda update anaconda
conda install spyder=5.0.5

Now when I restarted Spyder and ran my code it worked fine.

I have just installed and starting using anaconda so I don't know the root cause of this issue, but my guess is there seemed to be some "cross-connection" in the packages I had installed prior to my installation of Anaconda, and by running the above two steps now everything is running from within the Anaconda environment.

This error occurs when lxml is not installed, so just go to the terminal and run: pip3 install lxml

Terminal Image

I installed lxml 4.9.1, but it didn't work. So I tried to install lxml 4.8.0 instead, and it worked!

pip install lxml==4.8

I got the same problem. Trying to reinstall lxml does not work. After rereading the error message and tracing the error ~\Miniconda3\envs\mini_ds\lib\site-packages\pandas\io\html.py:872, I think I found the problem lies in the function _importers() in ~/pandas/io/html.py.

Here is the function:

def _importers() -> None:
    # import things we need
    # but make this done on a first use basis

    global _IMPORTS
    if _IMPORTS:
        return

    global _HAS_BS4, _HAS_LXML, _HAS_HTML5LIB
    bs4 = import_optional_dependency("bs4", errors="ignore")
    _HAS_BS4 = bs4 is not None

    lxml = import_optional_dependency("lxml.etree", errors="ignore")
    _HAS_LXML = lxml is not None

    html5lib = import_optional_dependency("html5lib", errors="ignore")
    _HAS_HTML5LIB = html5lib is not None

    _IMPORTS = True

You can see that for lxml option, it actually tries importing "lxml.etree" instead of "lxml". So this is probably why reinstalling "lxml" would not help.

Conclusion, I think this is perhaps a problem of pandas version (mine is 1.4.1). For me, a quick solution is to specify the flavor ='html5lib' in pd.read_html().

Related