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.

Leave a Comment