遇到问题: Windows下安装Python扩展模块, 提示“Unable to find vcvarsall.bat”的问题

80 篇文章 7 订阅
13 篇文章 1 订阅

全程参考:  https://www.cnblogs.com/yyds/p/7065637.html   写得很好,不知道能不能解决我的问题!!!

尝试解决办法:安装visual studio 2015, 原因请看下面。

1 为什么遇到这个问题:

1 在安装商汤出的PYSOT过程中遇到问题:
github程序PySOT(商汤推出的siam各种跟踪算法包)
我自己的博客 https://blog.csdn.net/zjc910997316/article/details/90749847

2 其中有个python扩展模块,里面用到了c语言的程序

报错如下:找不到 vcvarsall.bat

报错位置的文件
果然是 region.c文件
因此需要安装 Microsoft Visual C++ 或者 Visual Studio

 

2 为什么要安装 Microsoft Visual C++ 或者 Visual Studio:

问题1:为什么安装python扩展模块需要安装Microsoft Visual C++呢?
因为有些与操作系统底层密切相关的Python扩展,
由于使用C/C++ 进行代码编写,
因此在进行安装时需要进行C/C++ 代码的编译工作,
Windows平台的专用C/C++ 代码编译工具就是Microsoft Visual C++ ,
因此Python的模块管理工具(如,pip)默认设置的用来编译C/C++ 代码的工具就是VC

Linux平台上所使用的C/C++ 代码编译工具通常都是gcc,因此不涉及安装VS的问题。

问题2:为什么安装Visual Studio可以解决这个问题?
上面已经说明过了,因为Visual Studio中包含Visual C++
安装了Visual Studio之后也就安装了Visual C++。

问题3:为什么有时候安装Visual Studio最新版本都无法解决这个问题?
因为我们当前大部分使用的是CPython,
也就是C语言实现的Python版本,
我们在Windows上安装的Python也是经过VC编译过的可执行程序。

为了保证扩展模块的兼容性,
使用Python的模块管理工具(如,pip)安装C语言实现的外部扩展模块时,
会默认查找并使用与编译当前Python时所使用的相同内部版本或相互兼容的内部版本的的VC,
VS的内部版本与其所包含的VC的内部版本是一致的,
因此安装的VS版本过高或过低都可能会出现问题

3 什么是VS、什么是VC,以及它们对应关系:

 

4 如何确定需要安装哪个版本的Visual Studio呢?

环境1:如果  win10 + python27如下:
操作系统是Win 10,安装Python的目录是C://Python27
在Python的安装目录下可以找到这样一个Python文件:
C://Python27/Lib/distutils/msvccompiler.py
从文件名就能看出来这个Python文件就是用来处理与VC编译器有关的操作的。
在该Python文件中可以找到这样一个函数:

def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """

    prefix = "MSC v."
    i = string.find(sys.version, prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None

通过注释我们可以知道,这个函数就是用来确定编译当前Python所使用的VC编译器的版本号的,
而且注释中告知从Python 2.3版本开始,VC的版本信息已经包含在sys.version中了,对
于Python 2.3之前的版本就认为VC版本号是6。
 如下图所示:

“MSC V.”后面那个数字1500就是编译当前Python所使用的VC的版本信息
但还不是我们要找的那个内部版本号
我们要从这个版本信息中找到主版本号majorVersion次版本号minorVersion
主版本号majorVersion + 次版本号minorVersion的结果才是我们要找那个内部版本号
那么这里的majorVersion和minorVersion怎么获取到呢?
我们可以从上面的get_build_version()函数中找到答案:

   majorVersion = int(s[:-2]) - 6
    minorVersion = int(s[2:3]) / 10.0

上面代码中的 就是 'MSC v.'
后面那个数字1500,可见:

  • 前两位数减去6就是主版本号,即majorVersion = 15 - 6 = 9
  • 后面两位数除以10.0就是此版本号,即00 / 10.0 = 0.0
  • 所以我们可以得到我们要找的那个内部版本号:majorVersion + minorVersion = 9 + 0.0 = 9.0。
  • 从上面那个版本对应表中可以查到 内部版本号9.0对应的VC和VS名称分别是:Visual C++ 2008 和 Visual Studio 2008。

环境2(ps:我的环境):如果 像我一样 win7 + 我安装的是anaonda (python37)如下:

在该Python文件中可以找到这样一个函数:

def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """
    prefix = "MSC v."
    i = sys.version.find(prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    if majorVersion >= 13:
        # v13 was skipped and should be v14
        majorVersion += 1
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None

查看我的
VC的版本信息已经包含在sys.version中了
pyhton ->import sys -> sys.version

“MSC V.”后面那个数字1500就是编译当前Python所使用的VC的版本信息
但还不是我们要找的那个内部版本号
我们要从这个版本信息中找到主版本号majorVersion次版本号minorVersion
主版本号majorVersion + 次版本号minorVersion的结果才是我们要找那个内部版本号
那么这里的majorVersion和minorVersion怎么获取到呢?
我们可以从上面的get_build_version()函数中找到答案:

 majorVersion = int(s[:-2]) - 6
    minorVersion = int(s[2:3]) / 10.0

上面代码中的 就是 'MSC v.'
后面那个数字1915 1500,可见:

  • 前两位数减去6就是主版本号,即majorVersion = 19 - 6 = 13
  • 后面两位数除以10.0就是此版本号,即15 / 10.0 = 1.5
  • 所以我们可以得到我们要找的那个内部版本号:majorVersion + minorVersion = 13 + 1.5 = 14.5。
  • 从上面那个版本对应表中可以查到 内部版本号 14.5 对应的VC和VS名称分别是:Visual C++ 2015 和 Visual Studio 2015。

内部版本号14.5对应, 应该是VS2015吧~

 

5 我安装了VS2015但是打不开

安装了VS2015出现下面问题

百度上没有同样问题,从新安装之后成功安装!!!

此博客验证了安装VS2015的方法是可行的

https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/

但是安装VS2015对我帮助不大~~~还是找不到vcvarsall.bat!!!

6 于是我又参考了下面博客

https://blog.csdn.net/donger_soft/article/details/44838109

1 win+r 写入regedit 

2 下面有些路径没有是我自己建立的,现在问题是找到vcvarsall.bat????????

我的注册表也就卡在这了

PS:按照下面方法创建项,误在下面路径创建了productdir,可以删除

PS:下面一个评论

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:
        log.debug("Unable to find productdir in registry")
        productdir = None

    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 = 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

 

7 然后我按照此博客操作

https://blog.csdn.net/u011275279/article/details/73238609

法4:终极秘诀:微软社区的开放者对这个问题给出了答复。一句话:对于python,3.4 安装
Windows SDK for Windows 7 and .NET 4.0    连接:https://www.microsoft.com/en-us/download/details.aspx?id=8279 
(或者 Visual Studio 2010) 。对其他版本,见表:

详细见:https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/#comments

他搞定了,我还是不行!!!哎

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机视觉-Archer

图像分割没有团队的同学可加群

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值