131

I'm getting an error Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed? when trying to install lxml through pip.

  c:\users\f\appdata\local\temp\xmlXPathInitqjzysz.c(1) : fatal error C1083: Cannot open include file: 'libxml/xpath.h': No such file or directory
  *********************************************************************************
  Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
  *********************************************************************************
  error: command 'C:\\Users\\f\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

I don't find any libxml2 dev packages to install via pip.

Using Python 2.7 and Python 3.x on x86 in a virtualenv under Windows 10.

SaeX
  • 17,240
  • 16
  • 77
  • 97

13 Answers13

169

Install lxml from http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml for your python version. It's a precompiled WHL with required modules/dependencies.

The site lists several packages, when e.g. using Win32 Python 3.11, use lxml‑4.9.0‑cp311‑cp311‑win32.whl.

Download the file, and then install with:

pip install C:\path\to\downloaded\file\lxml‑4.9.0‑cp311‑cp311‑win32.whl
SaeX
  • 17,240
  • 16
  • 77
  • 97
  • a not for people having the same problem: 3.4.4. will result in the same error. only version 3.5.0 has gotten me rid of it – user151496 Dec 25 '15 at 00:54
  • 4
    @user151496 - yum install libxml2-devel libxslt-devel did this for me. http://stackoverflow.com/questions/26884514/why-cant-i-install-lxml-for-python – Trent Jan 15 '16 at 02:32
  • 2
    You can also install with this more generic command: pip install --use-wheel --find-links=C:\path\to\folder\with\wheel lxml – rleelr Oct 06 '16 at 14:38
  • Perfectly installed under Python 2.7 and W10. – m3nda Nov 07 '16 at 23:49
  • you should switch to linux – doesnt_matter Apr 03 '17 at 00:41
  • I have solved my problem installing `libxml2-dev` and `libxslt-dev` in Fedora – Cristhian Boujon Jun 12 '17 at 17:23
  • 2
    Great help. Another small thing I needed to do: It failed to install on my virtual env (wheel not supported). Eventually I've installed it on my regular python (c:\program files x86...) and copied the `python27\lib\site-packages\lxml*` folders to the virtual env. – reformy Sep 26 '18 at 08:58
  • 1
    gave me `lxml... is not a supported wheel on this platform.` – Inspi Dec 08 '22 at 09:37
  • @Inspi, match it with Python version installed. For Python version 3.11 it is `-cp311-`. – Philip Borbon Dec 09 '22 at 02:45
  • @Inspi: ensure you're using the right Python version and ensure you're using the right platform (Win32 <-> 64). Also this can be caused by an outdated PIP, so do a `python -m pip install --upgrade pip`. – SaeX Dec 09 '22 at 09:32
  • 1
    Excellent. I used amd64 version, and it worked on Windows 10 machine Thank you. – Michael Qin Dec 10 '22 at 05:35
  • It seems like https://www.lfd.uci.edu/~gohlke/pythonlibs/ is no longer updated. It says "Archived". – mzjn Feb 04 '23 at 14:29
166

I had this issue and realised that whilst I did have libxml2 installed, I didn't have the necessary development libraries required by the python package. Installing them solved the problem:

sudo apt-get install libxml2-dev libxslt1-dev
sudo pip install lxml
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61
practual
  • 2,363
  • 2
  • 11
  • 12
38

Try to use:
easy_install lxml
That works for me, win10, python 2.7.

Zing Lee
  • 720
  • 6
  • 10
28

On Mac OS X El Capitan I had to run these two commands to fix this error:

xcode-select --install
pip install lxml

Which ended up installing lxml-3.5.0

When you run the xcode-select command you may have to sign a EULA (so have an X-Term handy for the UI if you're doing this on a headless machine).

Jason D
  • 8,023
  • 10
  • 33
  • 39
  • 2
    Yup, that do the trick for me in OS X El Capitan. In fact, if you scrolls up the screen to see the root cause of the error, there is a line there says "Perhaps try: xcode-select --install" – Devy Jan 29 '16 at 22:18
  • This fixed it, and there is a line that does suggest this as commented above! – jfive Apr 05 '16 at 21:34
  • 1
    Does anyone else feel like they've run `xcode-select --install` before, though? Is there any introspection on what this does either a first or second time? As a note, though I did not have to install `lxml` globally for it to work in a virtual environment, so at least there is that. – lol Jun 13 '16 at 09:59
21

In case anyone else has the same issue as this on

Centos, try:

yum install python-lxml

Ubuntu

sudo apt-get install -y python-lxml

worked for me.

suryakrupa
  • 3,852
  • 1
  • 25
  • 34
J1MF0X
  • 689
  • 1
  • 6
  • 4
10
set STATICBUILD=true && pip install lxml

run this command instead, must have VS C++ compiler installed first

https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/

It works for me with Python 3.5.2 and Windows 7

BigDav
  • 149
  • 1
  • 3
2

I tried install a lib that depends lxml and nothing works. I see a message when build was started: "Building without Cython", so after install cython with apt-get install cython, lxml was installed.

gabrieloliveira
  • 560
  • 3
  • 10
2

I had this issue and realized that while I did have libxml2 installed, I didn't have the necessary development libraries required by the python package.

1) Installing them solved the problem:

The site to download the file: Download

2) After Installing the file save it in a accessible folder

pip install *path to that file*
2

For some reason it doesn't work in python 3.11, but 3.10 works.

On windows, to install a module with a previous version, use

py -3.10 -m pip install lxml

if you want to install it in a venv, then use

py -3.10 -m venv .venv
.venv/Scripts/pip.exe install lxml

if you've set up the venv, then you can just use

pip install lxml

You also need to run the python program with that version. If you set up a venv, then you don't need to do this.

py -3.10 file.py
1

It is not strange for me that none of the solutions above came up, but I saw how the igd installation removed the new version and installed the old one, for the solution I downloaded this archive:https://pypi.org/project/igd/#files

and changed the recommended version of the new version: 'lxml==4.3.0' in setup.py It works!

Ivan Minakov
  • 93
  • 1
  • 7
0

I got the same error for python 32 bit. After install 64bit, the problem was fixed.

Nishara MJ
  • 509
  • 4
  • 13
0

On MacOS Big Sur, I ran xcode-select --install as suggested, installed XCode, restarted the Mac, and was still getting the same error when installing lxml.

What helped was to reset the XCode path:

sudo xcode-select --reset

After that, lxml installed successfully.

Dennis Golomazov
  • 16,269
  • 5
  • 73
  • 81
  • why would you give a Mac answer to an almost 8 year old Windows question? very confused.. – Andre Greeff Jul 03 '23 at 20:09
  • 1
    @AndreGreeff I just had the same problem myself, and I'm on Mac, so I spent some time researching it and wanted to share my answer for people having the same problem. I know that the question is about Windows, but I see other non-Windows answers, so I thought mine can be helpful as well. – Dennis Golomazov Jul 28 '23 at 14:42
  • 1
    fair enough.. in hindsight, I guess this also helps to show future visitors that there was "more recent" activity on the issue. – Andre Greeff Jul 29 '23 at 17:43
-1

I am using venv.

In my case it was enough to add lxml==4.6.3 to requirements.txt.

One library wanted earlier version and this was causing this error, so when I forced pip to use newest version (currently 4.6.3) installation was successful.

Karol Zlot
  • 2,887
  • 2
  • 20
  • 37