Lxml is a HTML/XML processor for Python and it is needed for many projects, mostly used in parsing HTML pages. There are no competent guides for installing lxml on Windows so I’ll be your guide today to install it :)

You can't install this with pip. It will give many errors.

Before we start, you need:

  • Visual Studio 2008 and higher (we are going to build some C++ stuff)
  • Python 2.7 (I’m using this version)

Let’s start:

First, create a folder named lxml somewhere you want (C: drive is good choice).

(1) Download lxml source and put it in that folder.

(2) We need 4 dependencies: iconv, libxml2, libxslt, and zlib. Go to this page and download the ones that ends with win32.zip.

(3) Now we have downloaded all the ZIP files including lxml source, extract them so it will look something like this:

iconv-1.9.x.win32/
iconv-1.9.x.win32.zip
libxml2-2.x.x.win32/
libxml2-2.x.x.win32.zip
libxslt-1.x.x.win32/
libxslt-1.x.x.win32.zip
lxml-1.x.x/
lxml-1.x.x.tgz
zlib-1.x.x.win32/
zlib-1.x.x.win32.zip

xs you see here is the newest version you could find at the time.

Btw we are building the 32-bit version. You can't build 64-bit version because 64-bit version of iconv ZIP file is missing .a.lib file.

(4) Start up a new command prompt and go to lxml-1.x.x/.

(5) Type the command below based on the VS version you have:

  • SET VS90COMNTOOLS=%VS100COMNTOOLS% (if you have Visual Studio 2010)
  • SET VS90COMNTOOLS=%VS110COMNTOOLS% (if you have Visual Studio 2012)
  • SET VS90COMNTOOLS=%VS120COMNTOOLS% (if you have Visual Studio 2013)

(6) Enter this command (this is needed by CL.exe):

SET INCLUDE=C:\lxml\libxml2-2.x.x.win32\include\libxml2
Change the x.x with corresponding version.

(7) Open setup.py in your editor in lxml-1.x.x folder and find these lines:

STATIC_INCLUDE_DIRS = []
STATIC_LIBRARY_DIRS = []

And replace them with these:

STATIC_INCLUDE_DIRS = [
       "..\\libxml2-2.x.x.win32\\include",
       "..\\libxslt-1.x.x.win32\\include",
       "..\\zlib-1.x.x.win32\\include",
       "..\\iconv-1.x.x.win32\\include"
       ]

STATIC_LIBRARY_DIRS = [
       "..\\libxml2-2.x.x.win32\\lib",
       "..\\libxslt-1.x.x.win32\\lib",
       "..\\zlib-1.x.x.win32\\lib",
       "..\\iconv-1.x.x.win32\\lib"
       ]

(8) Last command: python setup.py bdist_wininst --static

After build finishes, you will find a setup file in dist folder something like this: lxml-3.3.0beta2.win32-py2.7.exe. You install that and you now have lxml installed in your python.

Change the x.x with corresponding version.

References

  • http://stackoverflow.com/questions/2817869/error-unable-to-find-vcvarsall-bat
  • http://stackoverflow.com/questions/10773732/compilation-error-in-visual-studio-linked-with-python26
  • http://stackoverflow.com/questions/3587316/cl-exe-exit-codes
  • http://lxml.de/3.0/build.html#static-linking-on-windows
  • http://stackoverflow.com/questions/13036249/need-help-building-a-debug-lxml-for-python27-on-windows