Cython:Unable to find vcvarsall.bat

VS与VC及内部版本的对应关系

VS名称 内部版本 VC名称
Visual Studio 4.0 Visual C++ 4.0
Visual Studio 97 5.0 Visual C++ 5.0
Visual Studio 6.0 6.0 Visual C++ 6.0
Visual Studio .NET 2002 7.0 Visual C++ 2002
Visual Studio .NET 2003 7.1 Visual C++ 2003
Visual Studio 2005 8.0 Visual C++ 2005
Visual Studio 2008 9.0 Visual C++ 2008
Visual Studio 2010 10.0 Visual C++ 2010
Visual Studio 2012 11.0 Visual C++ 2012
Visual Studio 2013 12.0 Visual C++ 2013
Visual Studio 2015 14.0 Visual C++ 2015
Visual Studio 2015 RTM 14.0 Visual C++ 2015

确定需要的VC版本号

Windows上安装的python也是经过VC编译过的可执行程序,为了保证扩展模块的兼容性,使用python的模块管理工具(pip)安装C语言实现的外部扩展模块时会默认查找并使用与编译当前python时所使用的相同内部版本或相互兼容的内部版本的VC。

Lib/distutils/msvc9compiler.py的函数get_build_version()

def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """
    prefix = "MSC v."
    i = sys.version.find(prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    if majorVersion >= 13:
        # v13 was skipped and should be v14
        majorVersion += 1
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None
>>> import sys
>>> sys.version
'3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]'

19-6=13 大于等于13,因此加1是14,像代码的注释里:# v13 was skipped and should be v14

选择安装Visual Studio 2015,安装完成后,系统环境变量里出现:
VS140COMNTOOLS: E:\Program Files\MicrosoftVisualStudio\Common7\Tools\
如果装的是VS 2017,是看不到出现环境变量的。

对于vcvarsall.bat的探测,VS2017以前的版本,可以通过VSXXXCOMNTOOLS环境变量来发现,例如:VS140COMNTOOLS对应 VS2015
但是VS2017之后就没这个环境变量了,因此更加通用可靠的探测方式,是从注册表中查找。
例如,VS2017的注册表项:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7;15.0)\VC\Auxiliary\Build
VS2017之前版本路径如下:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7;14.0)\VC
64bits版本,Microsoft之前再加个Wow6432Node。

执行:python setup.py build_ext –inplace
setup.py

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
    ext_modules = cythonize("dot_cython.pyx"),
)

依然出现Unable to find vcvarsall.bat,检查发现
E:\Program Files\MicrosoftVisualStudio\VC\路径下依然没有vcvarsall.bat,
因为安装Visual Studio 2015选的是默认,它并没有装Visual C++,打开Visual Studio,新建项目Visual C++,选择安装。

执行:python setup.py build_ext –inplace
接着出现错误

dot_cython.c(586): fatal error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file or directory
error: command 'E:\\Program Files\\MicrosoftVisualStudio\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2

在setup.py中加上

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
    ext_modules = cythonize("dot_cython.pyx"),
    include_dirs = [numpy.get_include()]
)
展开阅读全文

没有更多推荐了,返回首页