如何在Mac上安装Python的dlib?

9

我在尝试在macOS上使用Python 3.7安装dlib时遇到了错误。

pip3 install dlib

我已安装了CMake,所以那不是问题。
我收到了以下错误信息:
Failed building wheel for dlib

并且

Command "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/j2/nvk5521j2vn9s1w95_0vlwkm0000gn/T/pip-install-ls2e_3mr/dlib/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/j2/nvk5521j2vn9s1w95_0vlwkm0000gn/T/pip-record-hy3hu262/install-record.txt --single-version-externally-managed --compile" 
failed with error code 1 in /private/var/folders/j2/nvk5521j2vn9s1w95_0vlwkm0000gn/T/pip-install-ls2e_3mr/dlib/ 

在最后面标记为红色。我需要dlib是为了安装face_recognition。


它在我的Python 3.7.2,pip3 19.0.2,cmake 3.13.4,dlib 19.16.0和macOS 10.13.6上成功安装。有几个问题:(1)您是如何安装Python3和CMake的?(2)您的MacOS版本是什么? - Gino Mempin
1
我使用 pip3 install cmake 安装了 CMake。 我的操作系统是 macOS 10.14.3。几天前,我从网站安装了 Python 3.7.2。我正在使用CMake 3.13.3。 - owcs
在从许多SO帖子中尝试了多种组合的brew和pip卸载/重新安装cmake之后,包括完全删除brew并重新安装后,我降级了python3.11(通过brew将其删除)并安装了python 3.9。 which python3.9 which pip3.9 pip install cmakepip install dlib,最终终于成功了。 cmake --version == 3.25..2 - volvox
然后 pip install face_recognition 就可以正常运作了。所以基本上是 Python 升级似乎破坏了我在 Monterey 实例上的人脸识别脚本。 - volvox
6个回答

17

我认为错误信息可能涉及到CMake,要么它没有正确安装,要么与 pip3 install dlib 不兼容。

我建议使用 Homebrew 来安装Python3(包括pip3)和 CMake。Homebrew 管理软件包、工具、库的安装,这些可能依赖于系统相关的工具、路径等。它还可以避免您在系统上使用sudo安装软件的麻烦。

  1. 安装Homebrew

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  • 使用Homebrew安装Python3(和pip3)

  • $ brew install python@3.7  # or python@3.8 or python@3.9
    $ brew install python@3    # get whichever is the latest version
    
  • 检查Python安装

  • $ python3 -V
    $ python3 -m pip -V
    
  • 使用Homebrew安装CMake

  • 检查 CMake 安装

  • $ brew info cmake
    $ cmake --version
    
  • 最后,使用pip安装

  • $ python3 -m pip install dlib
    

    如果您不想使用Homebrew(出于某种原因),您可以尝试直接从这里安装CMake for Mac,使用安装程序(dmg或tar.gz文件): https://cmake.org/download/


    4
    1. Install Homebrew from here

    2. A bunch of dependencies are needed for dlib:

      brew install cmake
      brew install boost
      brew install boost-python
      brew install dlib
      pip3 install numpy
      pip3 install scipy
      pip3 install scikit-image
      pip3 install dlib
      
    3. If this does not work, try:

      python3 -m pip install dlib
      

    3

    dlib 依赖于 cmake,因此您需要先安装它。 您只需要在终端上运行以下命令:

    $ brew install cmake 
    $ pip install cmake 
    $ brew install dlib 
    $ pip install dlib
    

    “pip install dlib” 失败了,就像我在搜索中尝试的其他解决方案一样。 - volvox

    1

    您可以通过运行以下命令来安装conda-forge的Dlib包:


    conda install -c conda-forge dlib

    Conda-forge已确保解决了所有依赖冲突,因此我在安装过程中没有遇到任何问题。他们为Windows、Linux和Mac OS以及Python 2.7到3.8提供了预构建的二进制文件。当前包(在撰写本文时)基于Dlib v19.19。


    1
    $ pip3 install --upgrade pip
    
    $ pip3 install global
    
    $ pip3 install cmake
    
    $ pip3 install dlib
    

    $ brew install dlib
    

    $ sudo pip install dlib
    

    -1

    我想你需要做的唯一一件事就是用sudo运行pip3 install dlib命令。

    sudo pip3 install dlib


    1
    这些信息已经包含在其他答案中,例如这个。如果您没有要补充的内容,请仅点赞最佳答案。请阅读tour如何撰写好的答案? - Tomer Shetah

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接