Python工作笔记

Cython: Unable to find vcvarsall

2019-06-20  本文已影响0人  txfly

本文主要介绍如何解决“Unable to find vcvarsall.bat”错误。

问题

当使用Python3.7.2和VS2019时,Cython会出现“Unable to find vcvarsall.bat”的错误,原因是Python自带的打包工具distutils暂时不支持VS2019,或者找不到Microsoft Visual Studio Installer安装目录。最简单的解决办法是直接修改distutils目录下的_msvccompiler.py文件。

解决步骤

  1. C:\Program Files\Python\Lib\distutils\_msvccompiler.py复制到桌面,在函数_find_vc2017中注释掉异常返回部分,然后直接指定VS2019路径,添加path = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise"即可(我这儿使用的是企业版,如果是专业版,则路径为C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional),修改后的内容为:
def _find_vc2017():
    """Returns "15, path" based on the result of invoking vswhere.exe
    If no install is found, returns "None, None"

    The version is returned to avoid unnecessarily changing the function
    result. It may be ignored when the path is not None.

    If vswhere.exe is not available, by definition, VS 2017 is not
    installed.
    """
    import json

    root = os.environ.get("ProgramFiles(x86)") or os.environ.get("ProgramFiles")
    if not root:
        return None, None

    try:
        path = subprocess.check_output([
            os.path.join(root, "Microsoft Visual Studio", "Installer", "vswhere.exe"),
            "-latest",
            "-prerelease",
            "-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
            "-property", "installationPath",
            "-products", "*",
        ], encoding="mbcs", errors="strict").strip()
    except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
        # return None, None # 注释掉
        pass
    
    path = r"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise" # 指定版本路径
    path = os.path.join(path, "VC", "Auxiliary", "Build")
    if os.path.isdir(path):
        return 15, path

    return None, None
  1. 把修改后的_msvccompiler.py文件复制到C:\Program Files\Python\Lib\distutils目录下

测试环境

Python 3.7.2 + Cython 0.29.10 + VS2019

版权声明:本文为「txfly」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://www.jianshu.com/p/d432568133e4

上一篇下一篇

猜你喜欢

热点阅读