您的位置:首页 > 编程语言 > Python开发

解决一次error: Unable to find vcvarsall.bat的经历

2017-05-21 10:39 513 查看
在使用cx_freeze做window软件打包时,碰到了error: Unable to find vcvarsall.bat,然而这才是蛋疼的开始,经过一轮“百度”后,最终找到了一个自认为能解决的方案(基于python2.7+win7_x64),其他版本,不能保证哈,但估计思路差不多,敬请诸君参考!

1. 为什么会出现该错误,好吧,先扒扒Python的代码

定位到该文件:Python27\Lib\distutils\msvc9compiler.py(看自己的Python安装路径)

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")
print 'productdir', productdir, 'vsbase:',  vsbase
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
print 'toolskey', toolskey, 'productdir:', productdir, vsbase
toolsdir = os.environ.get(toolskey, None)

if toolsdir and os.path.isdir(toolsdir):
productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
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)

以上的代码可以看出,python是通过注册表的形式去定位到“vcvarsall.bat”这大哥所在路径的。所以我又特意把路径打印出来,如下:

VS90COMNTOOLS productdir: None Software\Microsoft\VCExpress\9.0

很明显我的目前的路径为空哈。。。。。几百个草泥马奔跑过!

当你看到VCExpress时,是不是觉得要去安装这个了?其实还是可以通过安装一个专门针对Python2.7的报,路径为:https://www.microsoft.com/en-us/download/details.aspx?id=44266

当你开心的安装好了,然后你觉得大功告成,然并卵,因为这个安装好,并不会自动帮你在注册表里登记哈。。。而且他还要安装在用户目录,所以只能通过搜索vcvarsall.bat位置,以下是我的路径:

C:\Users\username\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0

怎么解决,人肉加上呗:

错误原因:
报这个错误的原因是Python的distutils模块中的msvc9compiler.py并不从环境变量指定的路径中寻找’vcvarsall.bat’,而是通过注册表来寻找…,然而,不知为什么编译器安装过程没有配置注册表。

解决办法:
只要手工把注册表配置好,就可以了。
// 1、打开注册表编辑器
run regedit
// 2、配置
// 2.1、如果你安装的Python是32位的,则,创建如下项:
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Setup\VC

// 2.2、如果你安装的Python是64位的,则,创建如下项:
HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC

// 3、并在此项下新建字符串值:
名称:
productdir

数据:vcvarsall.bat所在路径
注意:路径中不包含最后的反斜杠。

此处解决方案有:
http://stackoverflow.com/questions/6551724/how-do-i-point-easy-install-to-vcvarsall-bat http://blog.csdn.net/donger_soft/article/details/44838109
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python
相关文章推荐