Python 3.6 + VS2017 + Anaconda 解決Unable to find vcvarsall.bat問題
已經安裝了VS2017,需要將項目中的C代碼翻譯為Python代碼,在編譯setup代碼時python setup.py build,出現了error: Unable to find vcvarsall.bat報錯。
-
找到VS2017的
vcvarsall.bat
文件,如C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat
-
打開Anaconda 的Lib文件夾下的
distutils
文件夾下的_msvccompiler.py
文件,如:C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\Lib\distutils\_msvccompiler.py
-
將
_find_vcvarsall(plat_spec)
函數內容替換為:
def _find_vcvarsall(plat_spec):
best_dir = r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build'
best_version = 17
vcruntime = None
vcruntime_spec = _VCVARS_PLAT_TO_VCRUNTIME_REDIST.get(plat_spec)
if vcruntime_spec:
vcruntime = os.path.join(best_dir,
vcruntime_spec.format(best_version))
if not os.path.isfile(vcruntime):
log.debug("%s cannot be found", vcruntime)
vcruntime = None
return r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat', vcruntime
- 問題1:為什么安裝python擴展模塊需要安裝Microsoft Visual C++呢?
因為有些與操作系統底層密切相關的Python擴展,由於使用C/C++ 進行代碼編寫,因此在進行安裝時需要進行C/C++ 代碼的編譯工作,而Windows平台的專用C/C++ 代碼編譯工具就是Microsoft Visual C++ ,因此Python的模塊管理工具(如,pip)默認設置的用來編譯C/C++ 代碼的工具就是VC。Linux平台上所使用的C/C++ 代碼編譯工具通常都是gcc,因此不涉及安裝VS的問題。
- 問題2:為什么安裝Visual Studio可以解決這個問題?
上面已經說明過了,因為Visual Studio中包含Visual C++,安裝了Visual Studio之后也就安裝了Visual C++。
- 問題3:為什么有時候安裝Visual Studio最新版本都無法解決這個問題?
因為我們當前大部分使用的是CPython,也就是C語言實現的Python版本,我們在Windows上安裝的Python也是經過VC編譯過的可執行程序。為了保證擴展模塊的兼容性,使用Python的模塊管理工具(如,pip)安裝C語言實現的外部擴展模塊時會默認查找並使用與編譯當前Python時所使用的相同內部版本或相互兼容的內部版本的的VC,而VS的內部版本與其所包含的VC的內部版本是一致的,因此安裝的VS版本過高或過低都可能會出現問題
Cython fatal error C1083: 無法打開包括文件: “numpy/arrayobject.h”: No such file or directory
在 setup
內添加 include_dirs=[np.get_include()]
,如:
from distutils.core import setup
from Cython.Build import cythonize
import numpy as np
setup(
ext_modules=cythonize("dtw.pyx"),
include_dirs=[np.get_include()]
)