[FIXED] Error: Unable to find vcvarsall.bat

Problem Statement: How to fix “Error: Unable to find vcvarsall.bat” in Python?

In this tutorial, we will to look at what vcvarsall.bat is in Visual Studio Code and how/when the Error: Unable to find vcvarsall.bat occurs in Python. We will also look at different approaches to solve the error.

What is vcvarsall.bat?

vcvarsall.bat is a Visual Studio Command Prompt tool used in Visual Studio. The vcvarshall.bat tool allows you to set different options for the IDE (integrated development environment). Additionally, it also allows you to build, debug and deploy the projects from the command line. vcvarsall.bat is an essential part of the Visual Studio Code compiler and is necessary to compile a module.

How Does The Error: Unable to find vcvarsall.bat Occur in Python?

The error Unable to find vcvarsall.bat appears on Windows systems majorly when you try to install or build a Python package that contains C codes/libraries. When you get this error, it basically means that you are trying to install a package that has an extension module, but only the source code is present, and Python is unable to compile this code. As we learned above that vcvarsall.bat is an essential part of the Visual Studio Code compiler that is necessary to compile a module; this means if it is missing, then the source code is written in another language (C, C++) would not be compiled, and you will get the error – “Unable to find vcvarsall.bat.”

Example: Suppose we are trying to install the dulwich Python package:

pip install dulwich

Output:

error: Unable to find vcvarsall.bat

The same thing happens when you try to install the package manually using setup.py:

python setup.py install
running build_ext
building 'dulwich._objects' extension
error: Unable to find vcvarsall.bat

Note:

  • You can integrate any code written in a compiled language like C, C++, or Java into a Python script. This code will be considered as an extension.
  • An extension module in Python is simply a C library.
    • The extension of these libraries on Unix is .so (shared object).
    • The extension of these libraries on Unix is .dll (dynamically linked library).

Well, how do we overcome this problem? Let’s dive into the fixes that can help us to get rid of this error.

#Fix 1: Use Python or Canopy’s 32-bit version

One way to solve this error would be to properly install MS Visual C++ 2008, the compiler which is used to compile Python 2.x itself. To take precautions that the error does not occur, you should compile with the same length (32 bit or 64 bit) as the version of the Canopy or Python where you wish to utilize the compiled C code. The most affordable situation, regardless of whether you are on 32 bit or 64 bit Windows, is to utilize Canopy’s 32-bit version, which by and large has identical functionality to the 64-bit Canopy’s version, except if you are working with large data arrays.

Note: Before you install anything which requires the C extensions, you just need to run the following batch file that will load the VC++ compiler’s environment into the session

32-bit Compilers:

“C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat”

64-bit Compilers:

The 64-bit compilers are in Program files.

“C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars64.bat”

Note: The difference between vcvars64.bat and vcvarsx86_amd64.bat or the difference between amd64 and x86_amd64, is that the former is used for the 64-bit compiler tools, whereas the latter is used for the 64-bit cross compilers that can also run on a 32 bit Windows installation.

#Fix 2: Set The Correct path in The Environment Variable

While installing the package manually using setup.py, Python 2.7 searches for an installed version of Visual Studio 2008. While this happens, you can trick the Python compiler into using a newer version of Visual Studio by just setting the correct path in the environment variable (VS90COMNTOOLS) before calling the setup.py. After that, you can execute one of the following commands based on the version of the Visual Studio installed:

Visual Studio 2010 (VS10): SET VS90COMNTOOLS = %VS100COMNTOOLS%

Visual Studio 2012 (VS11): SET VS90COMNTOOLS = %VS110COMNTOOLS%

Visual Studio 2013 (VS12): SET VS90COMNTOOLS = %VS120COMNTOOLS%

Visual Studio 2015 (VS14): SET VS90COMNTOOLS = %VS140COMNTOOLS%

#Fix 3: Installing a Microsoft Compiler

Sometimes you can fix this error by installing a Microsoft compiler that is compatible with the compiler used to build Python to solve the error: Unable to find vcvarsall.bat. It implies that you will need to install Visual C++ 2008 or newer versions. The error is automatically solved after this as Microsoft provides us with a bundle of compilers and headers to be able to compile the Python extensions. When you search for “Microsoft C++ redistributable 2010” or similar, you will find a direct link to download it from Microsoft.

#Fix 4: Being Blocked by an Antivirus Program

The error: Unable to find vcvarsall.bat can also occur as a “configure error: cannot run C compiled programs” when you are trying to install the package manually using setup.py.

The above error occurs when the antivirus installed on your system is blocking the execution of the freshly compiled .exe. Hence to solve this error, you have to disable the antivirus resident shield. However, you may still face an error as shown below:

cc1.exe: error: unrecognized command line option '-mno-cygwin' 
error: command 'gcc' failed with exit status 1

To solve this error, you have to install an older version of MinGW, or go to the Python directory and edit the distutils\cygwinccompiler.py and remove all the instances of -mno-cygwin.

#Fix 5: Adding mingw32’s bin Directory to The Environment variable

Another possible solution to this error is that you have to add the mingw32’s bin directory to an environment variable by appending the PATH with C:\programs\mingw\bin;

Further, you have to create the distutils.cfg located at: C:\Python27\Lib\distutils\distutils.cfg containing –

[build]
Compiler = mingw32

To deal with the error- MinGW not recognizing the -mno-cygwin flag, you just have to remove the flag in C:\Python27\Lib\distutils\cygwincompiler.py as follows:

self.set_executables(compiler = 'gcc -O -Wall',
                         compiler_so = 'gcc -mdll -O -Wall',
                         compiler_cxx = 'g++ -O -Wall',
                         linker_exe =' gcc',
                         linker_so = '%s %s %s'
                                    % (self.linker_dll, shared_option,
                                       entry_point))

We have come to the end of our discussion here. I hope this tutorial helped you. Please stay tuned and subscribe for more interesting articles and discussions.

Article contributed by: Shubham Sayon and Rashi Agarwal


Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!