Cython: Unable to find vcvarsall.bat

问题

当使用Python3.7.2和VS2019时,Cython会出现“Unable to find vcvarsall.bat”的错误,原因是Python自带的打包工具distutils暂时不支持VS2019,最简单的解决办法是直接修改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"即可,修改后的内容为:
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",
        ], encoding="mbcs", errors="strict").strip()
    except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
        return None, None
        
    path = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise"  #txf add
    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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值