psycopg2不能与docker一起安装

2024-05-20 02:03:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用djangorestframework和postgresql数据库开发一个应用程序。首先,我使用了alpine:python3.8图像,包括psycopg2,一切正常。然后,我需要使用机器学习软件包。我无法使用alpine:python3.8映像安装scipy、matplotlib、catsim等

因此,我将图像从alpine:python图像更改为alpine:3.7。除了安装psycopg2,我已经修复了许多错误。使用pip安装psycopg2时,出现错误:

In file included from psycopg/psycopgmodule.c:28:0:
    ./psycopg/psycopg.h:35:20: fatal error: Python.h: No such file or directory
     #include <Python.h>
                        ^
    compilation terminated.
    
    It appears you are missing some prerequisite to build the package from source.
    
    You may install a binary package by installing 'psycopg2-binary' from PyPI.
    If you want to install psycopg2 from source, please install the packages
    required for the build and try again.
    
    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).

我已经尝试了很多东西,阅读了所有与这个问题相关的问题。但是没有一个对我有效

DockerFile的内容是

FROM alpine:3.7
MAINTAINER Test

ENV PYTHONUNBUFFERED 1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

ENV PACKAGES="\
    dumb-init \
    musl \
    libc6-compat \
    linux-headers \
    build-base \
    bash \
    git \
    ca-certificates \
    freetype \
    libgfortran \
    libgcc \
    libstdc++ \
    openblas \
    tcl \
    tk \
    libssl1.0 \
    psycopg2-binary \
"

ENV PYTHON_PACKAGES="\
    numpy \
    matplotlib \
    scipy \
    scikit-learn \
    pandas \
    nltk \
"

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp-build-deps \
      gcc libc-dev linux-headers postgresql-dev
RUN apk add --update --no-cache postgresql-client
RUN apk add --virtual build-dependencies python3 --update py-pip \
RUN apk add --virtual build-runtime build-base build-essential postgresql-devel libpq-dev python3.8-dev libpython3.8-dev openblas-dev freetype-dev pkgconfig gfortran \
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h \
RUN pip3 install --upgrade pip \
RUN pip3 install $PYTHON_PACKAGES \
RUN apk del build-runtime \
RUN apk add --no-cache --virtual build-dependencies $PACKAGES \
RUN rm -rf /var/cache/apk/*

RUN pip3 install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN adduser -D user
USER user

requirements.txt文件的内容是

Django>=3.1.5,<3.2.5
djangorestframework>=3.12.2,<3.13.0
psycopg2>=2.8.5,<2.9.5
djangorestframework_simplejwt
django-cors-headers
requests
djangorestframework-datatables
django-filter

flake8<3.8.4,<3.9.0


Tags: installpipnorunfromdevbuildadd