windows安装pycocotools问题处理

项目中用到mmdetection、icevision等深度学习库,其中pycocotools这个依赖的安装,折腾了很久,直接看看遇到哪些问题了吧。

1.windows下直接安装pycocotools无法成功

pip install pycocotools这个安装直接报错,具体错误信息,还是需要源码安装时才能看到。

pip install pycocotools-windows这个库时可以直接安装成功的。安装成功后,部分依赖pycocotools的库是可以正常运行的,但是类似icevision这样的库,无法识别这个库,因此无法正常安装icevision。

2.pycocotools源码安装问题

2.1 C++编译环境类问题

这一类问题主要涉及C++编译环境的配置,包括(以下所列所在路径均依据windows kit和VS2019安装位置会有所改变)

  1. include路径查找不到指定的头文件
    1. io.h:cannot open include file: io.h’: no such file or directory
      所在路径C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt
    2. bsetsd.h:cannot open include file: ‘basetsd. H’: no such file or directory
      所在路径C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared
  2. 找不到指定的链接库lib
    1. Kernel32.lib: link: fatal error LNK1104: cannot open file ‘Kernel32.Lib’
      所在路径C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64
    2. ucrt.lib: link: fatal error LNK1104: cannot open file ‘ucrt.lib’
      所在路径C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64
  3. 找不到可执行应用
    1. cl.exe:error: command ‘cl.exe’ failed: No such file or directory
      所在路径D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx86\x64
      处理方法:cl.exe所在路径添加到系统环境变量
    2. rc.exe:fatal error LNK1158: cannot run ‘rc.exe‘
      所在路径C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64
      处理方法:复制win10 SDK rc.exe和rcdll.dll到VS目录下,具体目录可以根据错误提示推断,link.exe所在目录
  4. error: Unable to find vcvarsall.bat错误解决方式
    处理方法
    找到以下文件夹的文件(python安装路径):D:\Python37\Lib\distutils\ _msvccompiler.py,设置best_dir文件路径
    def _find_vcvarsall(plat_spec):
        # bpo-38597: Removed vcruntime return value
        _, best_dir = _find_vc2017()
        # 手动设置vcvarsall.bat查找路径
        best_dir = r"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build"
        if not best_dir:
            best_version, best_dir = _find_vc2015()
    
        if not best_dir:
            log.debug("No suitable Visual C++ version found")
            return None, None
    
        vcvarsall = os.path.join(best_dir, "vcvarsall.bat")
        if not os.path.isfile(vcvarsall):
            log.debug("%s cannot be found", vcvarsall)
            return None, None
    
        return vcvarsall, None
    

关于C++编译环境的配置,主要涉及的就是windows kit和MSVC的安装和配置。这个可以通过安装VS中C++相关的编译组件和windows 10 SDK解决。安装完成后,由于环境变量设置和setup.py中默认的查找位置,可能出现头文件和链接库等无法查找到的问题,这时可以通过以下几种方式解决:

  1. 把头文件或者链接库所在路径添加到系统环境变量。
  2. 把头文件所在路径添加到setup.py文件中的include_dirs参数中。
  3. 把链接库或可执行程序直接拷贝到报错信息提示的查找路径中。

2.2 pycocotools包自身问题

  • cocoapi官方实现:提供了lua\matlab\python操作coco数据集的API,但不支持windows环境使用。
  • pycocotools实现库:从cocoapi官方库fork过来,支持windows下使用,但是可能由于环境问题,无法直接安装成功。
  • pycocotools-windows实现库:同样从cocoapi官方库fork过来,支持windows下使用,大部分情况下可以直接安装使用。但是2.0.0.2版本之后不再更新,导致安装之后,不满足部分依赖库的要求,无法使用。
  1. 使用pycocotools实现库安装,只要解决了C++生成环境的相关问题,基本就能正常安装成功。
  2. 如果出现以下两个错误,可以检查以下setup.py文件中common文件配置的相对路径是否能够正确找到对应问题:
    1. fatal error C1083: 无法打开源文件: “../common/maskApi.c”: No such file or directory
    2. fatal error C1083: 无法打开源文件: “../common/_mask.c”: No such file or directory
  3. 源码安装命令与普通包安装有所不同
     # build_ext:build C/C++ extensions (compile/link to build directory),给python编译一个c、c++的拓展
     # ignore build-lib and put compiled extensions into the source directory alongside your pure Python modules,忽略build-lib,将编译后的扩展放到源目录中,与纯Python模块放在一起
     python setup.py build_ext --inplace
     python setup.py build_ext install
    
  4. 有的cocoapi的github库提供的版本时2.0,直接安装也是无法使用的。当时安装的时候遇到一个小技巧,可以直接更改setup.py文件中的version=2.0.4。由于是小版本,没有大的API差异,因此这样安装后也可以正常使用。

3. icevision导入问题处理

  1. 安装SpaceGrotesk-Medium.ttf字体
    下载地址安装位置:C:\Users\你的用户名.icevision\fonts

  2. 安装Arial.ttf字体
    下载地址
    安装位置:C:\Users\你的用户名\AppData\Roaming\Ultralytics

  3. 与lightning-flask结合的安装指令
    pip install icevision lightning-flash[image]

我没有创造知识,只是串联大佬们的经验,解决了自己的问题,记录下来。

参考博客

Cannot open include file: ‘io.h‘: No such file or directory

python pip on Windows - command ‘cl.exe’ failed

fatal error LNK1158: cannot run ‘rc.exe‘

【python】——setup.py build_ext --inplace命令解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱在一瞬间

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值