如何在Docker slim-bullseye中安装lxml

8yparm6h  于 2023-04-05  发布在  Docker
关注(0)|答案(1)|浏览(163)

我想把lxml python库安装到我的docker容器中
我的停靠文件

FROM python:3.9-slim-bullseye

WORKDIR /project

# forbid .pyc file recording
# forbid bufferization
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONBUFFERED=1

COPY . .

RUN apt-get update && apt-get install --no-install-recommends -y \
    gcc libc-dev libpq-dev  python-dev libxml2-dev libxslt1-dev python-lxml && apt-get install -y cron &&\
    pip install --no-cache-dir -r requirements.txt

根据这个问题How to install lxml on Ubuntu我应该安装

python-dev libxml2-dev libxslt1-dev python-lxml

但我有错误:

In file included from /usr/local/include/python3.9/code.h:13,
                     from /usr/local/include/python3.9/Python.h:113,
                     from src/lxml/etree.c:97:
    /usr/local/include/python3.9/cpython/code.h:117:28: note: declared here
      117 | PyAPI_FUNC(PyCodeObject *) PyCode_New(
          |                            ^~~~~~~~~~
    Compile failed: command '/usr/bin/gcc' failed with exit code 1
    cc -I/usr/include/libxml2 -I/usr/include/libxml2 -c /tmp/xmlXPathInitq8gr7w55.c -o tmp/xmlXPathInitq8gr7w55.o
    cc tmp/xmlXPathInitq8gr7w55.o -lxml2 -o a.out
    error: command '/usr/bin/gcc' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x119ixlv/lxml_3f16f39467334421824ee98edc9a857b/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x119ixlv/lxml_3f16f39467334421824ee98edc9a857b/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 /tmp/pip-record-y32de5p0/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/lxml Check the logs for full command output.
WARNING: You are using pip version 21.2.4; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
The command '/bin/sh -c apt-get update && apt-get install --no-install-recommends -y     gcc libc-dev libpq-dev  python-dev libxml2-dev libxslt1-dev && apt-get install -y cron &&    pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 1

如何修复此错误?是否需要更改图像?

qcbq4gxm

qcbq4gxm1#

只需将图像更改为

FROM python:3.9.6-buster

这修复了整个lxml问题。为了更好地衡量和避免更多关于bullseye中的wheel构建的问题,还将以下内容添加到您的Dockerfile中。

RUN apt-get -y install libxml2-dev libxslt1-dev python3-dev lib32z1-dev zlib1g-dev python3-lxml

相关问题