Tuesday, December 11, 2012

Python - error: Unable to find vcvarsall.bat

While I was woking on a django project, I had to use ImageField. To use ImageField it needed to install Python Imaging Library (PIL). I was working on a windows 64-bit machine and had Python 2.7.2 installed on the system. My django version was 1.4.1.

I generally use pip to install python libraries. So, when I typed in this install command:

pip install pil

It generated the following error.

error: Unable to find vcvarsall.bat

So, I did some research and found quite a few answers to all my questons. So, I thought i should compile them together so this might be useful for those who faced this problem too. In fact, it might be useful for me too in future. :)


The problem occurs probably because the library you are trying to install contains some C/C++ code. It needs to compile them. It tries to compile the source code Microsoft Visual Studio's compiler,  as you are working on a windows machine and it's unable to find it. It's a prerty common issue. Pythons distutils does not go well with MSVC Tool chain. Now, if you want to get around this problem there are a couple of soultions.


1. First solution is pretty straight forward. If you are in dire need of installing the library, don't have much time to do inquiries and don't care whatever caused the problem, you can just download a complied binary file and just install it in your system. You can find a lot of complied binaries of python libraries here. this is a huge collection.

2. Another solution is to use GCC to complie the library. You can use this library to install GCC which will configure python for you automatically.

3. Otherwise, you can use MinGW.
  • Download and install MinGW
  • Installation Directory: "C:\Program Files (x86)\MinGW"
  • Add it to your path variable. Go to Start Menu and type "edit environment variable"
    • Click on "edit the system environment variable". An window will open.
    • Click on Environment Variables. In the system variable section find the variable called PATH. 
    • Click on edit and add this line at the end "C:\Program Files (x86)\MinGW\bin".
  • Now whenever you face this error while installing a python library, you can tell the installer to use MinGW as compiler. 
    • Run the command prompt.
    • cd to the directory of the library source code where exists.
  • As you have set up the environment variable run install command and specify compiler to be MinGW32 by this command.
  • install build --compiler=mingw32
  • If you don't want to specify complier each time you install a new library or you want to use easy_install or pip you can set this in distutils configuration. Go to your python installation directory and edit the disutils.cfg file. If your python installation directory is "C:\Python27", you will find the file at "C:\Python27\Lib\distutils\distutils.cfg".
    Note that, distutils.cfg may not be there as default. In that case, create a blank file and rename it to distutils.cfg and add this lines:

4. If you are still reading this either you are too desperate or you don't like MinGW/GCC or you are a hardcore fan of MS Visual Studio. Well guess what, I have found a solution for you too.

  • Install a copy of MS Visual Studion 2010 Express
  • Open from "C:\Python27\Lib\distutils\"
  • replace line 243 with the following one
            toolskey = "VS100COMNTOOLS"
  • If you want to avoid manifest errors you can add this after line 647 (but this is optional).
  • Now you have to setup environment variable for Visual Studion so that python finds it. 
  • Say your installation directory is "C:\Program Files (x86)\Microsoft Visual Studio 10.0"
  • In your environment variable -> system variable -> Path variable add this value:
    "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\"
  • You can also set this globally in command prompt
    set VS100COMNTOOLS="C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\"

So, this is it. Hopefully you have solved your problem by now. :)

SyntaxHighlighter was used in this blog post. See this post for details.