如何解决Python安装dlib时C编译器编译失败问题?
安装dlib库失败求助
我需要安装dlib库以在Python中导入face_recognition模块,已完成以下配置:
- 安装cmake 3.29.1版本
- 安装Microsoft的Desktop development with C++工具
- 安装Python 3.12.2版本,且配置环境变量
C:\Users\mynama\AppData\Local\Programs\Python\Python312
执行pip install dlib时出现构建wheel失败的错误,提示C编译器C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe无法编译简单测试程序,具体错误输出如下:
pip install dlib Collecting dlib Using cached dlib-19.24.4.tar.gz (3.3 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: dlib Building wheel for dlib (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for dlib (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [143 lines of output] <string>:210: SyntaxWarning: invalid escape sequence '\(' <string>:211: SyntaxWarning: invalid escape sequence '\(' <string>:212: SyntaxWarning: invalid escape sequence '\(' running bdist_wheel running build running build_ext <string>:125: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead. Building extension for Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] Invoking CMake setup: 'cmake C:\Users\andre.pimentel\AppData\Local\Temp\pip-install-88y5mi18\dlib_5093a421ca20445ab77f1ec7f4eced53\tools\python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\andre.pimentel\AppData\Local\Temp\pip-install-88y5mi18\dlib_5093a421ca20445ab77f1ec7f4eced53\build\lib.win-amd64-cpython-312 -DPYTHON_EXECUTABLE=C:\Users\andre.pimentel\AppData\Local\Programs\Python\Python312\python.exe -DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\Users\andre.pimentel\AppData\Local\Temp\pip-install-88y5mi18\dlib_5093a421ca20445ab77f1ec7f4eced53\build\lib.win-amd64-cpython-312 -A x64' -- Building for: Visual Studio 17 2022 -- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045. -- The C compiler identification is MSVC 19.39.33523.0 -- The CXX compiler identification is MSVC 19.39.33523.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe - broken CMake Error at C:/Program Files/cmake-3.29.1-windows-x86_64/share/cmake-3.29/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe"
测试程序编译失败的详细输出:
Change Dir: 'C:/Users/andre.pimentel/AppData/Local/Temp/pip-install-88y5mi18/dlib_5093a421ca20445ab77f1ec7f4eced53/build/temp.win-amd64-cpython-312/Release/CMakeFiles/CMakeScratch/TryCompile-fy734e' Run Build Command(s): "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/MSBuild/Current/Bin/amd64/MSBuild.exe" cmTC_7bd2c.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:n MSBuild version 17.9.8+b34f75857 for .NET Framework Build started 4/8/2024 12:48:24 PM. Project "C:\Users\andre.pimentel\AppData\Local\Temp\pip-install-88y5mi18\dlib_5093a421ca20445ab77f1ec7f4eced53\build\temp.win-amd64-cpython-312\Release\CMakeFiles\CMakeScratch\TryCompile-fy734e\cmTC_7bd2c.vcxproj" on node 1 (default targets). PrepareForBuild: Creating directory "cmTC_7bd2c.dir\Debug\". Structured output is enabled. The formatting of compiler diagnostics will reflect the error hierarchy. See https://aka.ms/cpp/structured-output for more details. Creating directory "C:\Users\andre.pimentel\AppData\Local\Temp\pip-install-88y5mi18\dlib_5093a421ca20445ab77f1ec7f4eced53\build\temp.win-amd64-cpython-312\Release\CMakeFiles\CMakeScratch\TryCompile-fy734e\Debug\". Creating directory "cmTC_7bd2c.dir\Debug\cmTC_7bd2c.tlog\". InitializeBuildStatus: Creating "cmTC_7bd2c.dir\Debug\cmTC_7bd2c.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified. Touching "cmTC_7bd2c.dir\Debug\cmTC_7bd2c.tlog\unsuccessfulbuild".
解决方案
1. 补全VS Build Tools组件
打开Visual Studio Installer,修改Desktop development with C++的安装,确保勾选以下组件:
- MSVC v143 - VS 2022 C++ x64/x86生成工具
- Windows 10 SDK(匹配你的系统版本)
- C++ CMake工具 for Windows
- C++ ATL for latest v143 build tools
安装完成后重启电脑。
2. 用管理员权限执行安装
右键命令提示符或PowerShell,选择「以管理员身份运行」,再执行:
pip install dlib
3. 安装预编译wheel包
Python 3.12兼容性尚不完善,直接下载对应系统和Python版本的dlib预编译wheel文件,执行:
pip install 你的dlib文件名.whl
4. 指定编译参数安装
手动指定编译器和SDK版本,执行:
pip install dlib --global-option=build_ext --global-option="-DCMAKE_C_COMPILER=C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe" --global-option="-DCMAKE_CXX_COMPILER=C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe" --global-option="-DCMAKE_SYSTEM_VERSION=10.0.22621.0"
5. 降级Python版本
如果以上方法都无效,暂时降级到Python 3.11,再执行pip install dlib,这个版本和dlib的兼容性更稳定。
内容的提问来源于stack exchange,提问作者André Pimentel
相关产品推荐
相关产品推荐

