解决Windows 10下pip安装pycocotools报错“ERROR: Failed building wheel for pycocotools”

1. 问题描述

如题,在Windows 10 x64主机的Python Anaconda虚拟环境中安装pycocotools包时报错。安装命令为:

pip install pycocotools

执行后报错,如下所示:

Collecting pycocotools
  Using cached https://files.pythonhosted.org/packages/96/84/9a07b1095fd8555ba3f3d519517c8743c2554a245f9476e5e39869f948d2/pycocotools-2.0.0.tar.gz
Building wheels for collected packages: pycocotools
  Building wheel for pycocotools (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'C:\Users\test\Anaconda3\envs\cu100_py37_torch120\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\test\\AppData\\Local\\Temp\\pip-install-09qyqdlo\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\test\\AppData\\Local\\Temp\\pip-install-09qyqdlo\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\test\AppData\Local\Temp\pip-wheel-fx81fe0f' --python-tag cp37
       cwd: C:\Users\test\AppData\Local\Temp\pip-install-09qyqdlo\pycocotools\
  Complete output (19 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.7
  creating build\lib.win-amd64-3.7\pycocotools
  copying pycocotools\coco.py -> build\lib.win-amd64-3.7\pycocotools
  copying pycocotools\cocoeval.py -> build\lib.win-amd64-3.7\pycocotools
  copying pycocotools\mask.py -> build\lib.win-amd64-3.7\pycocotools
  copying pycocotools\__init__.py -> build\lib.win-amd64-3.7\pycocotools
  running build_ext
  building 'pycocotools._mask' extension
  creating build\temp.win-amd64-3.7
  creating build\temp.win-amd64-3.7\Release
  creating build\temp.win-amd64-3.7\Release\pycocotools
  creating build\temp.win-amd64-3.7\Release\common
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\test\Anaconda3\envs\cu100_py37_torch120\lib\site-packages\numpy\core\include -Icommon -IC:\Users\test\Anaconda3\envs\cu100_py37_torch120\include -IC:\Users\test\Anaconda3\envs\cu100_py37_torch120\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10150.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tcpycocotools/_mask.c /Fobuild\temp.win-amd64-3.7\Release\pycocotools/_mask.obj -Wno-cpp -Wno-unused-function -std=c99
  cl: 命令行 error D8021 :无效的数值参数“/Wno-cpp”
  error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
  ----------------------------------------
  ERROR: Failed building wheel for pycocotools
  Running setup.py clean for pycocotools
Failed to build pycocotools
Installing collected packages: pycocotools
  Running setup.py install for pycocotools ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\test\Anaconda3\envs\cu100_py37_torch120\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\test\\AppData\\Local\\Temp\\pip-install-09qyqdlo\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\test\\AppData\\Local\\Temp\\pip-install-09qyqdlo\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\test\AppData\Local\Temp\pip-record-wocuf7x5\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\test\AppData\Local\Temp\pip-install-09qyqdlo\pycocotools\
    Complete output (19 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.7
    creating build\lib.win-amd64-3.7\pycocotools
    copying pycocotools\coco.py -> build\lib.win-amd64-3.7\pycocotools
    copying pycocotools\cocoeval.py -> build\lib.win-amd64-3.7\pycocotools
    copying pycocotools\mask.py -> build\lib.win-amd64-3.7\pycocotools
    copying pycocotools\__init__.py -> build\lib.win-amd64-3.7\pycocotools
    running build_ext
    building 'pycocotools._mask' extension
    creating build\temp.win-amd64-3.7
    creating build\temp.win-amd64-3.7\Release
    creating build\temp.win-amd64-3.7\Release\pycocotools
    creating build\temp.win-amd64-3.7\Release\common
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\test\Anaconda3\envs\cu100_py37_torch120\lib\site-packages\numpy\core\include -Icommon -IC:\Users\test\Anaconda3\envs\cu100_py37_torch120\include -IC:\Users\test\Anaconda3\envs\cu100_py37_torch120\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10150.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tcpycocotools/_mask.c /Fobuild\temp.win-amd64-3.7\Release\pycocotools/_mask.obj -Wno-cpp -Wno-unused-function -std=c99
    cl: 命令行 error D8021 :无效的数值参数“/Wno-cpp”
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Users\test\Anaconda3\envs\cu100_py37_torch120\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\test\\AppData\\Local\\Temp\\pip-install-09qyqdlo\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\test\\AppData\\Local\\Temp\\pip-install-09qyqdlo\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\test\AppData\Local\Temp\pip-record-wocuf7x5\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.



2. 解决方法

S1. 在当前的虚拟环境中安装git,安装命令为:

conda install git

S2. 继续安装pycocotools包,安装命令为:

pip install git+https://github.com/philferriere/cocoapi.git#egg=pycocotools^&subdirectory=PythonAPI

注意: 可能因为文件总量较大,若中途报错远程连接断开并退出(类似fatal: the remote end hung up unexpectedly),可以多尝试几次。

S3. 至此,pycocotools包的安装就完成了。





参考资料:

  1. https://github.com/cocodataset/cocoapi/issues/169
  2. https://stackoverflow.com/questions/52920222/could-not-install-pycocotools-in-windows-fatal-error-c1083-cannot-open-include

你可能感兴趣的:(Python,环境搭建)