windows下,安装python扩展时报错:unable to find vcvarsall.bat

我在win10环境下安装一个python扩展时,报错如下:

错误原因:

%PythonInstallPath%\Lib\distutils\msvc9compiler.py 这个文件是Python用来处理与VC编译器有关的操作的。

其中该文件里的219行find_vcvarsall(version)函数中找不到vcvarsall.bat文件(报错信息就是这里出现的):说明python 编译器找不到计算机上面的 VC 编译器。

def find_vcvarsall(version):
    """Find the vcvarsall.bat file

    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    """
    vsbase = VS_BASE % version
    try:
        productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
                                   "productdir")
    except KeyError:
        log.debug("Unable to find productdir in registry")
        productdir = None

    if not productdir or not os.path.isdir(productdir):
        toolskey = "VS%0.f0COMNTOOLS" % version
        toolsdir = os.environ.get(toolskey, None)

        if toolsdir and os.path.isdir(toolsdir):
            productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
            productdir = os.path.abspath(productdir)
            if not os.path.isdir(productdir):
                log.debug("%s is not a valid directory" % productdir)
                return None
        else:
            log.debug("Env var %s is not set or invalid" % toolskey)
    if not productdir:
        log.debug("No productdir found")
        return None
    vcvarsall = os.path.join(productdir, "vcvarsall.bat")
    if os.path.isfile(vcvarsall):
        return vcvarsall
    log.debug("Unable to find vcvarsall.bat")
    return None

那么为什么安装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的问题。

VC版本问题:(下面代码是该文件中的一个函数,是用来确定VC版本的:)

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
  • 1)如果当前使用的是Python 2.7,可以安装Visual Studio 2008,也可以安装 Microsoft Visual C++ Compiler for Python 2.7来解决这个问题;
  • 2)如果当前当前使用的Python 3.x,只能通过安装相应版本的Visual Studio或Visual C++来解决这个问题。

下载地址:https://download.csdn.net/download/ejiao1233/10686973

参考文章链接:

Windows下安装Python扩展模块提示“Unable to find vcvarsall.bat”的问题

猜你喜欢

转载自blog.csdn.net/ejiao1233/article/details/82844332