前言

在 MacOS 系统下执行 pip3 install tesserocr 安装 tesserocr 时提示错误,错误信息看第二点。

本文记录一下解决方法,遇到类似问题的朋友可以参考下。

系统及软件环境:

  • MacOS Big Sur (Apple M1)
  • Python 3.9.10

错误提示

  • 报错截图

  • 具体报错信息
➜ ~ pip3 install tesserocr
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting tesserocr
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/66/01/05cdb20f2842993d134a1a52a34f45bfa40cc4b8480cce2cb654e2eff5ed/tesserocr-2.5.2.tar.gz (57 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: tesserocr
  Building wheel for tesserocr (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /opt/homebrew/opt/python@3.9/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/setup.py'"'"'; __file__='"'"'/private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-wheel-g767hddu
       cwd: /private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/
  Complete output (17 lines):
  Supporting tesseract v5.0.1
  Tesseract major version 5
  Building with configs: {'libraries': ['tesseract', 'lept'], 'compile_time_env': {'TESSERACT_MAJOR_VERSION': 5, 'TESSERACT_VERSION': 83886336}}
  /opt/homebrew/lib/python3.9/site-packages/setuptools/installer.py:27: SetuptoolsDeprecationWarning: setuptools.installer is deprecated. Requirements should be satisfied by a PEP 517 installer.
    warnings.warn(
  running bdist_wheel
  running build
  running build_ext
  Detected compiler: unix
  creating build
  creating build/temp.macosx-11-arm64-3.9
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c tesserocr.cpp -o build/temp.macosx-11-arm64-3.9/tesserocr.o -std=c++11 -DUSE_STD_NAMESPACE
  tesserocr.cpp:737:10: fatal error: 'leptonica/allheaders.h' file not found
  #include "leptonica/allheaders.h"
           ^~~~~~~~~~~~~~~~~~~~~~~~
  1 error generated.
  error: command '/usr/bin/clang' failed with exit code 1
  ----------------------------------------
  ERROR: Failed building wheel for tesserocr
  Running setup.py clean for tesserocr
Failed to build tesserocr
Installing collected packages: tesserocr
    Running setup.py install for tesserocr ... error
    ERROR: Command errored out with exit status 1:
     command: /opt/homebrew/opt/python@3.9/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/setup.py'"'"'; __file__='"'"'/private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-record-bgl0a73l/install-record.txt --single-version-externally-managed --compile --install-headers /opt/homebrew/include/python3.9/tesserocr
         cwd: /private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/
    Complete output (19 lines):
    Supporting tesseract v5.0.1
    Tesseract major version 5
    Building with configs: {'libraries': ['tesseract', 'lept'], 'compile_time_env': {'TESSERACT_MAJOR_VERSION': 5, 'TESSERACT_VERSION': 83886336}}
    /opt/homebrew/lib/python3.9/site-packages/setuptools/installer.py:27: SetuptoolsDeprecationWarning: setuptools.installer is deprecated. Requirements should be satisfied by a PEP 517 installer.
      warnings.warn(
    running install
    /opt/homebrew/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
      warnings.warn(
    running build
    running build_ext
    Detected compiler: unix
    creating build
    creating build/temp.macosx-11-arm64-3.9
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c tesserocr.cpp -o build/temp.macosx-11-arm64-3.9/tesserocr.o -std=c++11 -DUSE_STD_NAMESPACE
    tesserocr.cpp:737:10: fatal error: 'leptonica/allheaders.h' file not found
    #include "leptonica/allheaders.h"
             ^~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    error: command '/usr/bin/clang' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /opt/homebrew/opt/python@3.9/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/setup.py'"'"'; __file__='"'"'/private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-install-k_b2ld1l/tesserocr_61ff9b96fff14da0a89beeedeb4ae07f/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/33/q5zvjrv965905rrfts89gsbc0000gn/T/pip-record-bgl0a73l/install-record.txt --single-version-externally-managed --compile --install-headers /opt/homebrew/include/python3.9/tesserocr Check the logs for full command output.
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip' command.

解决办法

执行下方命令,安装 pkg-config 这个程序:

brew install pkg-config

安装成功

提示⚠️:

  1. 如果没有 brew 命令,请先安装 Homebrew 工具。
  2. 什么是 pkg-config, 可看 pkg-config 介绍
  3. 为什么要安装 pkg-config? 可看 tesserocr 2.5.2 【Linux and BSD/MacOS】 部分内容。

安装成功后,再次安装 tesserocr:

pip3 install tesserocr

成功安装:

最后修改:2022 年 05 月 05 日 02 : 08 PM