本文主要介绍如何解决“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
文件。
解决步骤
- 把
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
- 把修改后的
_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
网友评论