tag:blogger.com,1999:blog-84977853111701191612024-03-08T16:45:20.818+06:00ad infinitum et ultraKnowledge is valuable and it requires time to acquire them, which is also valuable. This blog is a collection of knowledge which I have acquired spending time and energy but I tend to forget. The purpose is to just keep track of how I have solved the different types of problems I sometimes stumble upon.Tahmid Rafihttp://www.blogger.com/profile/10197951836877543979noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-8497785311170119161.post-31086643718173032182012-12-11T02:34:00.001+06:002013-09-23T23:44:09.373+06:00Python - error: Unable to find vcvarsall.bat<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
While I was woking on a <a href="https://www.djangoproject.com/">django</a> project, I had to use <a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ImageField">ImageField</a>. To use <a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ImageField">ImageField</a> it needed to install <a href="http://www.pythonware.com/products/pil/">Python Imaging Library (PIL)</a>. I was working on a windows 64-bit machine and had <a href="http://www.python.org/">Python</a> 2.7.2 installed on the system. My <a href="https://www.djangoproject.com/">django </a>version was 1.4.1.<br />
<br />
I generally use <a href="http://pypi.python.org/pypi/pip">pip</a> to install python libraries. So, when I typed in this install command:</div>
<br />
<pre class="brush: python">pip install pil</pre>
<br />
It generated the following error. <br />
<br />
<pre class="brush: python">error: Unable to find vcvarsall.bat</pre>
<br />
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. :)<br />
<br />
<span style="font-size: large;">Reason:</span><br />
<br />
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 <a href="http://code.google.com/p/rdflib/issues/detail?id=104http://code.google.com/p/rdflib/issues/detail?id=104">issue</a>. 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.<br />
<br />
<span style="font-size: large;">Solution:</span><br />
<br />
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.<br />
<br />
<a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/">http://www.lfd.uci.edu/~gohlke/pythonlibs/</a>
<br />
<div>
<br /></div>
2. Another solution is to use GCC to complie the library. You can use <a href="http://www.develer.com/oss/GccWinBinaries">this library</a> to install GCC which will configure python for you automatically.<br />
<a href="http://stackoverflow.com/questions/11726517/error-unable-to-find-vcvarsall-bat-when-installing-lingpy1-0-1-in-python-2-7x">source</a><br />
<br />
3. Otherwise, you can use MinGW.<br />
<ul style="text-align: left;">
<li>Download and install <a href="http://sourceforge.net/projects/mingw/files/">MinGW</a>. </li>
<li>Installation Directory: "C:\Program Files (x86)\MinGW"</li>
<li>Add it to your path variable. Go to Start Menu and type "edit environment variable"</li>
<ul>
<li>Click on "edit the system environment variable". An window will open.</li>
<li>Click on Environment Variables. In the system variable section find the variable called PATH. </li>
<li>Click on edit and add this line at the end "C:\Program Files (x86)\MinGW\bin".</li>
</ul>
<li>Now whenever you face this error while installing a python library, you can tell the installer to use MinGW as compiler. </li>
<ul>
<li>Run the command prompt.</li>
<li><i>cd</i> to the directory of the library source code where <i>setup.py</i> exists.</li>
</ul>
<li>As you have set up the environment variable run <i>install</i> command and specify compiler to be <b>MinGW32</b> by this command.</li>
<pre class="brush: python">setup.py install build --compiler=mingw32</pre>
<li>If you don't want to specify complier each time you install a new library or you want to use <i>easy_install</i> or <i>pip</i> you can set this in distutils configuration. Go to your python installation directory and edit the <b>disutils.cfg </b>file. If your python installation directory is "C:\Python27", you will find the file at "C:\Python27\Lib\distutils\distutils.cfg".<br />
Note that, distutils.cfg may not be there as default. In that case, create a blank file and rename it to <b>distutils.cfg</b> and add this lines:<br />
<pre class="brush: python">[build]
compiler=mingw32</pre>
</li>
</ul>
<a href="http://blog.eddsn.com/2010/05/unable-to-find-vcvarsall-bat/">source</a><br />
<br />
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.<br />
<br />
<ul style="text-align: left;">
<li>Install a copy of MS Visual Studion 2010 Express</li>
<li>Open msvc9compiler.py from <code>"C:\Python27\Lib\distutils\msvc9compiler.py"</code></li>
<li>replace line 243 with the following one<br />
<pre class="brush: python"> toolskey = "VS100COMNTOOLS"
</pre>
</li>
<li>If you want to avoid manifest errors you can add this after line 647 (but this is optional).<br />
<pre class="brush: python"> ld_args.append('/MANIFEST')
</pre>
</li>
<li>Now you have to setup environment variable for Visual Studion so that python finds it. </li>
<li>Say your installation directory is "C:\Program Files (x86)\Microsoft Visual Studio 10.0"</li>
<li>In your environment variable -> system variable -> <b>Path</b> variable add this value:<br />
<code>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\"</code></li>
<li>You can also set this globally in command prompt<br />
<pre class="brush: python">set VS100COMNTOOLS="C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\"</pre>
</li>
</ul>
<a href="http://web.archive.org/web/20101221084639/http://nukeit.org/compile-python-2-7-packages-with-visual-studio-2010-express/">source</a><br />
<br />
So, this is it. Hopefully you have solved your problem by now. :)<br />
<br />
SyntaxHighlighter was used in this blog post. See <a href="http://blog.cartercole.com/2009/10/awesome-syntax-highlighting-made-easy.html">this post </a>for details.<br />
<br /></div>
Tahmid Rafihttp://www.blogger.com/profile/10197951836877543979noreply@blogger.com4