Building lxml for Python 2.7 on Windows
I bet you're not using VS 2008 for this :)
There's def find_vcvarsall(version): function (guess what, it looks for vcvarsall.bat) in distutils with the following comment
At first it tries to find the
productdir of VS 2008 in the registry.
If that fails it falls back to the
VS90COMNTOOLS env var.
If you're not using VS 2008 then you have neither the registry key nor suitable environment variable and that's why distutils can't find vcvarsall.bat file. It does not check if the bat file is reachable through the PATH environment variable.
The solution is to define VS90COMNTOOLS variable to point to Tools directory of Visual Studio.
That being said take a look at 11.4. distutils.msvccompiler — Microsoft Compiler section in Python's docs which states
Typically, extension modules need to
be compiled with the same compiler
that was used to compile Python.
Martin v. Loewis in the email titled Download Visual Studio Express 2008 now on python-list mailing list states the same
Python 2.6, 2.7, and 3.1 are all built
with that release (i.e. 2008). Because
of another long tradition, Python
extension modules must be built with
the same compiler version (more
specifically, CRT version) as Python
itself. So to build extension modules
for any of these releases, you need to
have a copy of VS 2008 or VS 2008
Express.
In the light of above statements you should use VS 2008 if you want to build lxml for Python 2.7 so although setting VS90COMNTOOLS takes care of finding vcvarsall.bat file it's not the solution.
That being said :) people do try to use older CRT with newer compiler:
Can I use Visual Studio 2010's C++ compiler with Visual Studio 2008's C++ Runtime Library?
How to Enforce C++ compiler to use specific CRT version?
VS 2008 - Link against older C runtime
I'd like to thank Kev Dwyer (for pointing out importance of version of VS which is used) and Stefan Behnel (for pointing me to distutils as a place dealing with compiler's configuration) in the thread Problem building lxml under Windows - error: Unable to find vcvarsall.bat on lxml mailing list. I'd like to also thank agronholm from freenode #distutils IRC channel for confirmation that distutils does contain code which looks for vcvarsall.bat file.
How to install lxml on Windows
First, following the comments, I downloaded the lxml-3.4.2-cp34-none-win_amd64.whl
file and tried to open it with a pip install
, but it just told me it wasn't a valid wheel file on my system or something.
Then, I downloaded the win_32
file and it worked! Maybe it's because I have an Intel processor and AMD64 is, unsurprisingly, only for AMD processors.
Can't install lxml in python2.7
lxml
depends on various C libraries, and you have to have those C libraries installed—including their development files (headers, .so
or .a
libraries, etc.)—to build lxml
. The installation docs explain what prerequisites you need to build on your particular platform.
This error:
/usr/bin/ld: cannot find -lz
… means that the prerequisite you're missing is libz
, aka zlib
.
Since you're not on Windows, it's incredibly unlikely that you actually don't have zlib
at all… but pretty plausible that you don't have the development files for zlib
. On some platforms, most notably many linux distros, packages are typically split into separate pieces. For example, the parts of zlib
needed at runtime may be in a package named zlib
, while the parts needed for building other programs that need zlib
in a package named zlib-dev
or zlib-devel
. The exact details depend on your exact platform.
That being said, if you don't have the zlib
development files, you probably don't have the libxml2
or libxslt
development files either, because I don't know of any platform where installing those wouldn't have pulled in the zlib
files as well.
At any rate, since you haven't told us what platform you're one (and distro, if linux), I don't know what package manager to use, what the packages are named, etc., but do whatever's appropriate for your platform.
Also:
I already have
python-lxml
installed
You really shouldn't install the same package both with your distro's package manager and with pip
; that's a great way to confuse yourself.
But at any rate, most likely you installed python-lxml
from a binary package, not from source, which means you didn't need the build requirements. Now you're trying to build it from source, which means you do.
installing lxml on 64 bit windows
By default, Python will install as 32-bit on Windows, even on 64-bit systems. Therefore, to match this, you must select the wheel for 32-bit systems.
You can verify that your Python interpreter is 32-bit, because the REPL will look like this when starting:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
You should pick the lxml-3.6.4-cp35-cp35m-win32.whl
package for Python 3.5 on Windows (unless you explicitly selected 64-bit when downloading Python) which should solve your issue.
install lxml package. I am using python 2.7.5 in windows 7
The best way to do this is to download it form this, website. It provides some unofficial binaries. Chose the right python version and install.
Related Topics
How to One Hot Encode Variant Length Features
Set Up Python Simplehttpserver on Windows
Password Authentication in Python Paramiko Fails, But Same Credentials Work in Ssh/Sftp Client
Lambda Function Don't Closure the Parameter in Python
Ambiguity in Pandas Dataframe/Numpy Array "Axis" Definition
Generalise Slicing Operation in a Numpy Array
Get First Row Value of a Given Column
How to Use MySQLdb with Python and Django in Osx 10.6
Too Many Values to Unpack Calling Cv2.Findcontours
How to Overlay Plots from Different Cells
Replace Negative Values in an Numpy Array
How to Sort a Pandas Dataframe by Index
When to Use Sys.Path.Append and When Modifying %Pythonpath% Is Enough
How to Get Md5 Sum of a String Using Python
How to Efficiently Handle European Decimal Separators Using the Pandas Read_CSV Function