Python error Unable to find vcvarsall.bat error is the most impressive problem I’ve encountered when installing Python packages on Windows platforms. This solution was compiled on September 10, 2012. Nine years have passed, and I believe there are still many of you who encountered similar problems. I took the time to recreate the previous solution and sort it out. The main change is to add the solution under Python2.

Problem Cause

If the package/module you installed has contents written in cpython, you need to compile the middle C code into a binary file before the installation can be completed successfully.

Solutions

Option 1: Select the compiled wheel file to install

Preparation: install wheel support, pip install wheel

Follow-up: find the corresponding .whl installation package (http://www.lfd.uci.edu/~gohlke/pythonlibs/)

Installation: Install directly using pip install xxx.whl, where xxx is the file path.

Caution.

  • Not all packages have corresponding binary packages because they are compiled by unofficial organizations.
  • When choosing a package, you need to determine the version of Python you are installing and whether the Python you are installing is 32-bit or 64-bit.

Option 2: Install Microsoft’s compilation environment Visual Studio

Python 2.6 to 3.2

Direct installation of Visual Studio 2008 (tested, ready to use without configuration) or Microsoft Visual C++ Compiler for Python 2.7 (not tested)

Python 3.3 and 3.4

Install Windows SDK for Windows 7 and .NET 4.0 (not tested) or Visual Studio 2010 (some configuration required after installation)

Open “<python installation directory>\Lib\distutils\msvc9compiler.py”, modify the msvc9compiler.py file, and set: vc_env = query_vcvarsall(VERSION, plat _spec) set VERSION to the value corresponding to the installed version of VS.

  • VS2008, then VERSION is 0
  • VS2010, then VERSION is 0
  • VS2012, then VERSION is 0
  • VS2013, then VERSION is 0
  • VS2014, then VERSION is 0

Python 3.5 and later

Visual C++ Build Tools 2015 (not tested) or Visual Studio 2015

Option 3: Install MinGW compilation environment

Since installing Visual Studio takes up too much space, I personally prefer to install MinGW: * Download and install MinGW

  • Download and install MinGW

  • Find the bin folder under the installation directory of MinGW, find mingw32-make.exe, copy it and rename it to exe

  • add the path of MinGW to the environment variable path, for example, if I install MinGW into D:\MinGW, add D:\MinGW\bin to the path.

  • Add the file cfg to <python installation directory>\distutils, enter the following in the file and save it

    1
    2
    
    [build]
    compiler=mingw32
    
  • Execute the original module installation, found or error, the error content: error: command ‘gcc’ failed: No such file or directory Solution is to add D:\MinGW\lib to the PATH again.

  • If error: Could not find ‘openssl.exe’ appears during the installation process, download and install it directly from https://pypi.org/project/pyOpenSSL.

  • When installing the module when executing again, the following error is found.

    1
    2
    3
    4
    5
    
    D:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall “-ID:\Program Files\Python27\inc
    lude” “-ID:\Program Files\Python27\include” “-ID:\Program Files\Python27\PC” -c
    ../libdasm.c -o build\temp.win32-2.7\Release\..\libdasm.o
    cc1.exe: error:unrecognized command line option ‘-mno-cygwin’
    error: command ‘gcc’ failed with exit status 1
    

The reason is that gcc 4.6.x no longer accepts -mno-cygwin after that. To fix this problem, you need to modify the <python installation directory>\distutils\cygwinccompiler.py file. Find.

1
2
3
4
5
6
7
self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
                             compiler_so='gcc -mno-cygwin -mdll -O -Wall',
                             compiler_cxx='g++ -mno-cygwin -O -Wall',
                             linker_exe='gcc',
                             linker_so='%s -mno-cygwin %s %s'
                                        % (self.linker_dll, shared_option,
                                           entry_point))

Modify to read:

1
2
3
4
5
6
7
self.set_executables(compiler='gcc -O -Wall',
                             compiler_so='gcc -mdll -O -Wall',
                             compiler_cxx='g++ -mno-cygwin -O -Wall',
                             linker_exe='gcc',
                             linker_so='%s -mno-cygwin %s %s'
                                        % (self.linker_dll, shared_option,
                                           entry_point))