Python2.7.9跑在virtualenv中。
先总结网上说的三种方法:
1. 安装 mingw
2. 安装vs2010然后设置环境变量
3. 有人Python3下安装wheel后成功解决:见知乎https://www.zhihu.com/question/26857761,不过我是Python2,很不幸还是不行。
解决:
1. 先找Root Reason
首先肯定了系统确实没有vcvarsall.bat!
然后调查发现Python的Lib\distutils\msvc9compiler.py中的find_vcvarsall()函数中,最终因为找不到路径,返回该错误码。
vcvarsall = os.path.join(productdir, "vcvarsall.bat")
if os.path.isfile(vcvarsall):
return vcvarsall
log.debug("Unable to find vcvarsall.bat")
其中vcvarsall由productdir而来,productdir由toolsdir和toolskey生成,toolskey是环境变量,经调试,我这里toolskey为VS90COMNTOOLS
系统里根本没有VS90COMNTOOLS环境变量。
2. 不想因为装个Python就安装VS 2008或者2010。经过搜索,可以通过安装Microsoft Visual C++ Compiler for Python 2.7 解决问题,安装之,系统中找了vcvarsall.bat,有进步,说明方向是正确的。安装后设置了VS90COMNTOOLS变量,指向Compiler 的路径。
注意安装环境变量后要重启MS Dos
3. 问题仍没最终解决,调试结果为productdir路径不对,后来想想,原来productdir的路径都是依照VS 2008的逻辑设置的。没有考虑到我这种懒得安装VS的情形。怎么办,修改代码,将productdir指向正确路径。完整的修改后find_vcvarsall()函数,只改了一行。
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:
productdir = None
# trying Express edition
if productdir is None:
vsbase = VSEXPRESS_BASE % version
try:
productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
"productdir")
except KeyError:
productdir = None
log.debug("Unable to find productdir in registry")
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 = toolsdir
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
4. 在想是不是有空把这个问题issue发给Python,促使其完善代码哈。
评论
No comments yet.