问题
当使用Python3.7.2和VS2019时,Cython会出现“Unable to find vcvarsall.bat”的错误,原因是Python自带的打包工具distutils
暂时不支持VS2019,最简单的解决办法是直接修改distutils
目录下的_msvccompiler.py
文件
解决步骤
- 把
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
- 把修改后的
_msvccompiler.py
文件复制到C:\Program Files\Python\Lib\distutils
目录下
测试环境
Python 3.7.2 + Cython 0.29.10 + VS2019