摘自:https://www.biaodianfu.com/python-error-unable-to-find-vcvarsall-bat.html
在安裝一些Python模塊時,大部分是cpython寫的模塊時會發生如下錯誤 error: Unable to find vcvarsall.bat。先前的一篇文章:在Windows上安裝Scrapy時也講到了這個問題。當時講到的方案是,安裝VS 2008進行解決,但是Vs 2008又太大,不想裝,所以這次想到了另外的方案,同樣是上次說的,當時上次很不完整。
方案一:安裝Vs2008(實測)
完全的無腦流,安裝完問題直接解決。
方案二:安裝Vs2010(2016-1-29更新)
上次在電腦上裝個Vs2010並不能像 vs2008那樣直接解決問題,主要原因是Python 2.7 使用的是 VS 2008編譯的,所以Python 2.7默認只能認出VS 2008。
解決辦法,在命令行下執行 SET VS90COMNTOOLS=%VS100COMNTOOLS%
- VS 2010 對應:SET VS90COMNTOOLS=%VS100COMNTOOLS%
- VS 2012 對應:SET VS90COMNTOOLS=%VS110COMNTOOLS%
- VS 2013 對應:SET VS90COMNTOOLS=%VS120COMNTOOLS%
或者通過修改Python的源代碼進行修改:打開“<python安裝目錄>\Lib\distutils\msvc9compiler.py”,找到 toolskey = “VS%0.f0COMNTOOLS” % version,直接修改為 toolskey = “VS100COMNTOOLS”
如果是Python 3,則上面的方法是無效的,原因是Python 3使用的是VS 2010編譯的,所以設置應該是這樣:
- VS 2010 無需設置,直接能認出
- VS 2012 對應:SET VS100COMNTOOLS=%VS110COMNTOOLS%
- VS 2013 對應:SET VS100COMNTOOLS=%VS120COMNTOOLS%
或修改msvc9compiler.py文件,將: vc_env = query_vcvarsall(VERSION, plat_spec) 中的VERSION設定為已安裝的VS版本對應的值:
- VS2008,則VERSION為9.0
- VS2010,則VERSION為10.0
- VS2012,則VERSION為11.0
- VS2013,則VERSION為12.0
- VS2014,則VERSION為13.0
注意:Python 3.5升級了distutils,默認使用_msvccompiler.py,在這個文件中可以找到:“ if version >= 14 and version > best_version: ”這里的14說明VS版本要在14以上才可以。所以根據這句,我們要安裝最新的Visual Studio2015。上面修改msvc9compiler.py的辦法沒有效果。
另外,微軟也提供了解決方案:
Python Version | You will need |
3.5 and later | Visual C++ Build Tools 2015 or Visual Studio 2015 |
3.3 and 3.4 | Windows SDK for Windows 7 and .NET 4.0 (Alternatively, Visual Studio 2010 if you have access to it) |
2.6 to 3.2 | Microsoft Visual C++ Compiler for Python 2.7 |
參考鏈接:https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/
解決方案三:安裝MinGW(實測)
1、下載安裝MinGW,下載地址為:http://sourceforge.net/projects/mingw/files/latest/download?source=files
2、在MinGW的安裝目錄下找到bin文件夾,找到mingw32-make.exe,復制一份更名為make.exe
3、把MinGW的路徑添加到環境變量path中,比如我把MinGW安裝到D:\MinGW\中,就把D:\MinGW\bin添加到path中;
4、在<python安裝目錄>\distutils增加文件distutils.cfg,在文件里輸入
[build]
compiler=mingw32
保存;
5、執行原先的模塊安裝,發現還是報錯,報錯內容為:error: command ‘gcc’ failed: No such file or directory 解決方案是將D:\MinGW\lib再添加到PATH中。
6、如果安裝過程中出現 error: Could not find ‘openssl.exe’ 則直接到http://pypi.python.org/pypi/pyOpenSSL/0.13 下載安裝即可。
7、再次執行時安裝模塊時,發現如下錯誤:
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
原因是gcc 4.6.x 以后不再接受-mno-cygwin為了解決這個問題需要修改<python安裝目錄>\distutils\cygwinccompiler.py文件。找到:
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))
|
修改為:
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))
|
至此,大功告成!
方案四:安裝編譯好的wheel文件
先安裝好wheel: pip install wheel
尋找對應的.whl文件:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
使用 pip install filename.whl 進行安裝