【问题标题】:Unable to install PyMuPDF on alpine docker image无法在 alpine docker 映像上安装 PyMuPDF
【发布时间】:2020-12-21 06:34:14
【问题描述】:

我正在尝试在 apline 图像上安装 pymupdf 包,但出现以下错误

fitz/fitz_wrap.c:2739:10: fatal error: ft2build.h: No such file or directory
     2739 | #include <ft2build.h>
          |          ^~~~~~~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
 RUN pip install PyMuPDF
 ---> Running in 34d246d6f01b
Collecting PyMuPDF
  Downloading PyMuPDF-1.18.5.tar.gz (251 kB)
Using legacy 'setup.py install' for PyMuPDF, since package 'wheel' is not installed.
Installing collected packages: PyMuPDF
    Running setup.py install for PyMuPDF: started
    Running setup.py install for PyMuPDF: finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-uxc_zm2j/pymupdf/setup.py'"'"'; __file__='"'"'/tmp/pip-install-uxc_zm2j/pymupdf/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-nipvlcn8/install-record.txt --single-version-externally-managed --compile --install-headers /usr/include/python3.8/PyMuPDF
         cwd: /tmp/pip-install-uxc_zm2j/pymupdf/
    Complete output (20 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.8
    creating build/lib.linux-x86_64-3.8/fitz
    copying fitz/__init__.py -> build/lib.linux-x86_64-3.8/fitz
    copying fitz/fitz.py -> build/lib.linux-x86_64-3.8/fitz
    copying fitz/utils.py -> build/lib.linux-x86_64-3.8/fitz
    copying fitz/__main__.py -> build/lib.linux-x86_64-3.8/fitz
    running build_ext
    building 'fitz._fitz' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/fitz
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/include/mupdf -I/usr/local/include/mupdf -Imupdf/thirdparty/freetype/include -I/usr/include/python3.8 -c fitz/fitz_wrap.c -o build/temp.linux-x86_64-3.8/fitz/fitz_wrap.o
    fitz/fitz_wrap.c:2739:10: fatal error: ft2build.h: No such file or directory
     2739 | #include <ft2build.h>
          |          ^~~~~~~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-uxc_zm2j/pymupdf/setup.py'"'"'; __file__='"'"'/tmp/pip-install-uxc_zm2j/pymupdf/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-nipvlcn8/install-record.txt --single-version-externally-managed --compile --install-headers /usr/include/python3.8/PyMuPDF Check the logs for full command output.
WARNING: You are using pip version 20.2.4; however, version 20.3.3 is available.
You should consider upgrading via the '/usr/bin/python3.8 -m pip install --upgrade pip' command.
The command '/bin/sh -c pip install PyMuPDF' returned a non-zero code: 1

【问题讨论】:

  • 您好,可以分享一下您的 docker 文件吗?我们没有足够的信息,但这似乎是您的包含目录的问题。你是用 pip 安装 PyMuPDF 吗?你在使用 python 3 吗?点子是最新的吗?
  • 是的,我们使用的是 python3 版本,pip 也是最新版本
  • 并不是所有的包都可以在alpine中运行,我认为pymupdf是其中之一,你可以安装freetype-dev,设置ENV C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include/freetype2来解决这个问题,但你最终还是会遇到其他的告诉你错误来自源代码的错误...

标签: python docker alpine pymupdf


【解决方案1】:

Alpine 中的 freetype-dev 将安装在 /usr/include/freetype2 中。您可以将标题和目录链接到 /usr/include apk add mupdf-dev安装mupdf,方法结构与pymupdf不一致,需要从源码编译mupdf库。

FROM python:3.8-alpine
RUN apk add gcc g++ cmake make mupdf-dev freetype-dev
ARG MUPDF=1.18.0
RUN ln -s /usr/include/freetype2/ft2build.h /usr/include/ft2build.h \
    && ln -s /usr/include/freetype2/freetype/ /usr/include/freetype \
    && wget -c -q https://www.mupdf.com/downloads/archive/mupdf-${MUPDF}-source.tar.gz \
    && tar xf mupdf-${MUPDF}-source.tar.gz \
    && cd mupdf-${MUPDF}-source \
    && make HAVE_X11=no HAVE_GLUT=no shared=yes prefix=/usr/local install \
    && cd .. \
    && rm -rf *.tar.gz mupdf-${MUPDF}-source
RUN pip install PyMuPDF==1.18.9

试试这个,效果很好

此外,Alpine 的官方 Mupdf 库已更新,使安装更容易。(更新于 2021-04-28)

FROM python:3.8-alpine
# Add temporary virtual environment dependencies
RUN apk --no-cache add --virtual .builddeps gcc g++

# These dependency packages cannot be removed because they continue to be used in PyMupdf
RUN apk --no-cache add  mupdf-dev freetype-dev jbig2dec-dev jpeg-dev openjpeg-dev 

# install PyMupdf
RUN pip install pymupdf

# Remove virtual environment dependencies
RUN apk del .builddeps

【讨论】:

  • 请解释你的答案
  • freetype-dev 将安装在 /usr/include/freetype2 中,需要手动链接到 /usr/include。从源代码安装 mupdf 库需要 mupdf-dev 库。编译mupdf的时候需要编译成共享库,这样安装pymupdf的时候会被检测到
  • 我正在使用 python:3.7-alpine3.13,经过几个小时的尝试,我可以确认这个(并且只有这个)有效。 @ghoul,谢谢!
【解决方案2】:

对于这些类型的错误,可以采取简单的步骤:

  1. 读取错误并识别丢失的文件,您已经完成了,您似乎缺少ft2build.h
  2. 转到Alpine package website 并浏览内容标签
  3. 文件字段中输入您缺少的文件的名称;在 Branch 中,选择您的 Alpine 版本,然后在 Repository 中选择 main
  4. 这将指向一个特定的包,只需通过 apk 在你的 Dockerfile 中安装它,你应该很高兴

在您的情况下,这是在 Alpine 版本 3.12 上进行此类搜索的结果:

因此您可以在现有的apk 命令中添加包freetype-dev 来修复您的问题

RUN apk add --no-cache \
      freetype-dev

【讨论】:

  • 我已经尝试过使用 freetype-dev pacakge 但仍然遇到同样的错误
  • 谢谢你,比谷歌搜索每一个缺失的依赖要好得多,这就是我一直在做的事情
【解决方案3】:

尝试先用pip install -U pip更新pip。

这是对问题的深入探讨https://github.com/pymupdf/PyMuPDF/issues/894 有一个从 tar.gz 文件而不是 .whl 获取源的旧 pip,并且由于 freetype2 的错误 INCLUDE dir 导致构建失败,这是自 PyMuPDF 1.18 以来的解决方法。 2 但是... Pypi 的源 tar.gz 已过时。

【讨论】:

    【解决方案4】:

    问题是 PyPI 上没有用于 docker 文件建立的环境的轮子。 在这种情况下,pip 会尝试从源代码创建 PyMuPDF。 因为 PyMuPDF 是 MuPDF 的 绑定,所以当没有安装 MuPDF 时,这反过来必然会失败……就像这里一样。 因此解决方案是

    • 在安装 PyMuPDF 之前安装 MuPDF
    • 或使用有轮子的操作系统/Python 组合之一。

    【讨论】:

    • 与 Centos 7 相同——不仅与 docker 相同。我无法安装 MuPDF 或 PyMuPDF。为什么不为这些操作系统提供轮子,或者根本不可能?这将使每个人都更简单,因为到目前为止,无论提供什么解决方案,我都无法安装 MuPDF。
    • 如果你连MuPDF都无法安装,那么PyMuPDF当然就没有希望了。根据过多的互不兼容的 Linux 风格:这就是为什么存在轮平台标签 manylinux 的原因。很难相信它们都不适用于您的情况顺便说一句。 Github Actions 或 Travis CI try 等持续集成服务通过为流行案例提供操作系统来提供帮助。我正在使用 Github Actions,它的支持范围最大。在 PyMuPDF 的主页github.com/pymupdf/PyMuPDF 上发布您的问题。那里会更容易解决。
    • @Robin 另一条评论:最新的manylinux 轮子是基于 CentOS 7 构建的!所以这使你的困难成为一种奇怪的现象。您只需应该在 PyPI 上找到合适的轮子。如果您确实不这样做:您的机器/Python 不是 64 位的吗?
    • 感谢您的回答。非常有趣的信息。我使用带有 python 3.6 和 3.8 的 64 位机器。我会根据你的 cmets 尝试另一个轮子。
    • 因为 Python 3.6 现在是僵尸版本,所以没有更多的轮子支持。希望你能理解。但是 3.8 的轮子应该可以工作!
    【解决方案5】:

    这对我有用,仅适用于 python:3.8.10 或更高版本以及 Pymupdf:1.18.14:

    1)。仅当您没有安装 python 时:
    apk add --update py-pip

    2)。安装依赖:

    apk add --no-cache \
    python3-dev \
    mupdf-dev \
    gcc \
    libc-dev \
    musl-dev \
    jbig2dec \
    openjpeg-dev \
    jpeg-dev \
    harfbuzz-dev
    

    3)。创建快捷方式:
    ln -s /usr/lib/libjbig2dec.so.0 /usr/lib/libjbig2dec.so

    4)。安装 PymuPdf:
    pip install pymupdf

    仅当您想同时进行时

    apk add --update py-pip \
    && apk add --no-cache \
    python3-dev \
    mupdf-dev \
    gcc \
    libc-dev \
    musl-dev \
    jbig2dec \
    openjpeg-dev \
    jpeg-dev \
    harfbuzz-dev \
    && ln -s /usr/lib/libjbig2dec.so.0 /usr/lib/libjbig2dec.so \
    && pip install pymupdf
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 2023-01-14
      • 2022-08-18
      • 2018-07-13
      • 2020-10-17
      • 1970-01-01
      • 2021-11-07
      相关资源
      最近更新 更多