Windows下Python 3.6 + VS2017 + Anaconda 解決Unable to find vcvarsall.bat問題
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()]
)