如何在Windows 11上安装pycocotools(实操记录)

参考
https://blog.csdn.net/m0_45971439/article/details/118332681
https://blog.csdn.net/en_Wency/article/details/124767742

一、报错内容

该报错出现在安装yolov5依赖包时

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

详细报错信息

Building wheels for collected packages: pycocotools
  Building wheel for pycocotools (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for pycocotools (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [23 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-cpython-38
      creating build\lib.win-amd64-cpython-38\pycocotools
      copying pycocotools\coco.py -> build\lib.win-amd64-cpython-38\pycocotools
      copying pycocotools\cocoeval.py -> build\lib.win-amd64-cpython-38\pycocotools
      copying pycocotools\mask.py -> build\lib.win-amd64-cpython-38\pycocotools
      copying pycocotools\__init__.py -> build\lib.win-amd64-cpython-38\pycocotools
      running build_ext
      cythoning pycocotools/_mask.pyx to pycocotools\_mask.c
      building 'pycocotools._mask' extension
      creating build\temp.win-amd64-cpython-38
      creating build\temp.win-amd64-cpython-38\Release
      creating build\temp.win-amd64-cpython-38\Release\common
      creating build\temp.win-amd64-cpython-38\Release\pycocotools
      "d:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.33.31629\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\JMan\AppData\Local\Temp\pip-build-env-d_ei2r35\overlay\Lib\site-packages\numpy\core\include -I./common -Id:\Users\JMan\anaconda3\envs\yolov5\include -Id:\Users\JMan\anaconda3\envs\yolov5\Include "-Id:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.33.31629\include" "-Id:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" /Tc./common/maskApi.c /Fobuild\temp.win-amd64-cpython-38\Release\./common/maskApi.obj
      maskApi.c
      ./common/maskApi.c(8): fatal error C1083: 无法打开包括文件: “math.h”: No such file or directory
      C:\Users\JMan\AppData\Local\Temp\pip-build-env-d_ei2r35\overlay\Lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\Users\JMan\AppData\Local\Temp\pip-install-jawtp7pt\pycocotools_d75225a67ebb4faa97736024f57f7e57\pycocotools\_mask.pyx
        tree = Parsing.p_module(s, pxd, full_module_name)
      error: command 'd:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.33.31629\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for pycocotools
Failed to build pycocotools
ERROR: Could not build wheels for pycocotools, which is required to install pyproject.toml-based projects

这个报错在上次安装yolov5依赖包时也出现过,可以见https://blog.csdn.net/weixin_45246566/article/details/127619175?spm=1001.2014.3001.5501
然而仔细查看报错内容可以发现两次报错的原因是不同的,这次问题的根本原因是【Windows不能直接安装pycocotools】
下面记录一下曲折离奇的Windows安装pycocotools(差点翻车)过程。
(说实话,写到这儿都不知道能不能安明白,也不确定这篇文章能不能写出来。)

二、Windows11上安装pycocotools

1.下载pycocotools源码

  • 在这里下载 https://github.com/cocodataset/cocoapi
  • 将下载解压后的文件夹直接放入带配置环境下的site-packages
    我这里是要在新创建的环境yolov5下安装,目录为:D:\Users\JMan\anaconda3\envs\yolov5\Lib\site-packages

2.安装vs

这里安装的是2022community,因为之前安装了vs installer,所以直接在里面下载。
这里需要注意的地方就是要勾选 使用C++的桌面开发 和 Windows11 SDK(SDK在单个组件中,win10系统就选win10 SDK)
在这里插入图片描述

3.配置vs的环境变量

在很多教程中不涉及这一部分,但我在经历了无数次报错无法解决后可以肯定这一步骤是很重要的!以下操作都是在用户变量中进行。(设置-高级系统设置-环境变量)

  • (1)找到路径C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64
    就是顺着vs的安装路径找,找不到安装路径可以右键快捷方式,查找文件所在位置。
    把上面这个路径添加到用户变量的Path
  • (2)新建一个名为INCLUDE的用户变量
    添加路径C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include
    然后定位到 Windows Kits 文件夹,如果你把应用装在D盘,你就会在D盘找到D:\Windows Kits路径,而如果你的应用在C盘那就在路径C:\Program Files (x86)\Windows Kits继续进入路径C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0这一串数字是你安装的SDK的版本号。
    在这里插入图片描述
    将该文件夹下的五个文件夹的路径依次存入INCLUDE中,就像这样:
    在这里插入图片描述
  • (3)新建一个名为LIB的用户变量
    填写这个路径(对应你自己的):C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\lib\x64
    再去到Windows Kits文件夹,找到路径C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0
    在这里插入图片描述
    将里面每个文件夹里的x64文件夹的路径写入变量LIB中,像这样:
    在这里插入图片描述
    以上环境变量就配置完成啦!
    可以验证一下是否配置成功: win+r输入cmd打开命令提示符窗口,输入cl,出现下图则配置成功:
    在这里插入图片描述

4.编译

进入yolov5环境(因为我要在这个环境下安装),然后进到刚才复制到site-package文件夹下的PythonAPI路径下,运行下面两条指令:

python setup.py build_ext --inplace
python setup.py build_ext install

就可以了。
在这里插入图片描述

三、报错记录

报错基本上是出现在执行python setup.py build_ext --inplace命令时。

扫描二维码关注公众号,回复: 17185329 查看本文章

1.无效的数值参数

cl: 命令行 error D8021 :无效的数值参数“/Wno-cpp” 和 cl: 命令行 error D8021 :无效的数值参数“/Wno-unused-function”

这里是说setup.py文件中有无效的参数,解决的办法是将setup.py中的这一行注释掉,比较神奇的是,我第二次下载的cocoapi-master文件已经直接将这行注释掉了。这个文件就在PythonAPI文件夹下。
在这里插入图片描述

2.找不到头文件’math.h’

这个报错的截图找不到了,后来配置了vs的环境变量后就不再报错了。

3.缺少xxx包

  • ModuleNotFoundError: No module named 'Cython'
    像这种就直接pip install Cython
  • AttributeError: module 'numpy' has no attribute 'get_include'
    这里提示numpy包有问题,也是uninstall后再install,少的包都install后就解决了。

【写在最后】:这是一个很艰辛的过程,在临近毕设中期的紧张时刻浪费了我很多时间,不过也算是成功从良莠不齐的百度资料中试错成功。文章内容是在我安装成功后集中写的,可能有细节遗漏,因此仅供参考,主要是写给自己作为备忘。

猜你喜欢

转载自blog.csdn.net/weixin_45246566/article/details/129111069