Install lxml on windows (on a virtualenv)
Lxml is a nice python library for XML processing. ETree
is really quick, which makes things interesting if you have a large amount of XML files (or a bigger one) to process. Installation on linux/mac is painless (OK, you need homebrew on mac to make int painless, but you get my point…).
The other day I had to do it on windows. This is not really painless. I managed to do it, so here are the steps.
Prerequisites
- Python 2.7
- VC++ 2880, the Express edition (freely downloadable from Microsoft), because python 2.7 is compiled against VS 2008
- Create a virtual environment (e.g. in
C:\Users\myuser\virtualenvs\myvenv
). I’ll refer to this path asvirtual_env_path
later in the article. - In your virtual environment directory, create:
- A directory PC (e.g.
virtual_env_path\PC
). This will contain the headers (equivalent to include). - A directory “PCbuild”. This will contain the libraries (equivalent to “lib”).
- A directory PC (e.g.
Installing dependencies
To be successful, you need to install several dependencies. The procedure is identical and it’s outlined below:
- Download the archive (.zip file). All archives have similar structure, typically containing directories named bin, lib and include
- Place the files in the archive’s include directory in the PC directory created above. Note that your path should read e.g.
virtual_env_path\PC\libxml
, notvirtual_env_path\PC\include\libxml
, - Place the files of the lib directory in virtual_env_path\PCbuild.
Lxml has four dependencies which need to be installed following the steps described above:
- Install libxml from xmlsoft.org
- Install libxslt from xmlsoft.org
- Install zlib from gnu
- Install libiconv from gnu, namely the “developer files” zip. Important: copy “libiconv.lib” to “iconv.lib” in PCbuild.
Once this is done, you can install lxml in your virtual environment:
1 |
pip install lxml |
As a side note, you need to perform the following steps:
- Get the default iconv archive, because it contains the .DLL file
- Copy all contents of bin directories in downloaded archives to your virtual environment’s bin directory
- copy the libiconv.dll to iconv.dll
- copy all the .dll files in your
virtual_env_path\Lib\site-packages\lxml
. This is definitely not nice :(.
A little experiment: If you find this post and ad below useful, please check the ad out :-)
Hey, thanks for the tutorial! It just worked for me, however I was confused with Step 4, I didn’t do the last part of it. However it worked flawlessly still.
Glad to hear it worked :)
Could you explain the steps in the ‘side note’ a little more?
“Copy all contents of bin directories in downloaded archives to your virtual environment’s bin directory” – we never made a bin directory in the previous steps
“copy the libiconv.dll to iconv.dll” – where is this libiconv.dll file? I only have libiconv.dll.a
“copy all the .dll files in your virtual_env_path\Lib\site-packages\lxml. This is definitely not nice :(.” – What is this virtual_env_path\Lib\site-packages\lxml? We never made it in the previous steps. Should we make this folder …\site-packages\lxml folder, and then take all the .dll files found in the PC and PCBuild folders, and copy them into …\lxml? Or how do you mean, “copy all the .dll files in your …\lxml”?
Thank you very much for making these steps available for everyone. I hope they can help me :) So far it doesn’t work, but perhaps because I wasn’t able to follow the side note.
Thanks for the detailed comment. I’ll have a look and update the post :)
I’ll take it point by point.
virtual_env_path\Lib\site-packages\lxml
directoryHTH,