Home > Mobile >  Install psycopg2 on pypy3 docker image
Install psycopg2 on pypy3 docker image

Time:10-03

I tried very hard to install psycopg2 on pypy3 docker image https://hub.docker.com/_/pypy

This proved impossible...

FROM pypy:3
WORKDIR /
COPY . .
RUN apt-get update
RUN apt-get install libpq-dev gcc
RUN apt-get -y install python3-dev
RUN apt-get -y install postgresql
RUN apt-get -y install gcc
RUN pypy3 -m pip install --upgrade pip
RUN pypy3 -mpip install psycopg2            <-- Blow up here
RUN pypy3 -mpip install -r requirements.txt
ENV PATH /opt/conda/bin:$PATH
ENV PYTHONPATH "${PYTHONPATH}:/src"
ENTRYPOINT ["pypy3", "xxx.py"]

Error message is:

api_gateway_1         |   File "/opt/pypy/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 793, in dbapi
api_gateway_1         |     import psycopg2
api_gateway_1         | ModuleNotFoundError: No module named 'psycopg2'

esql -I/usr/include/postgresql/13/server -I/usr/include/libxml2 -c psycopg/typecast.c -o build/temp.linux-x86_64-3.7/psycopg/typecast.o -Wdeclaration-after-statement
#8 1044.6     In file included from psycopg/typecast.c:168:
#8 1044.6     ./psycopg/typecast_datetime.c: In function ‘_parse_inftz’:
#8 1044.6     ./psycopg/typecast_datetime.c:116:14: error: ‘PyDateTime_TimeZone_UTC’ undeclared (first use in this function); did you mean ‘PyDateTime_Time’?
#8 1044.6       116 |     tzinfo = PyDateTime_TimeZone_UTC;
#8 1044.6           |              ^~~~~~~~~~~~~~~~~~~~~~~
#8 1044.6           |              PyDateTime_Time
#8 1044.6     ./psycopg/typecast_datetime.c:116:14: note: each undeclared identifier is reported only once for each function it appears in
#8 1044.6     error: command 'gcc' failed with exit status 1

I've tried different things, none of them worked. https://blog.csdn.net/cdnight/article/details/52536451 https://cloud.tencent.com/developer/ask/119621 Installing psycopg2 command 'gcc' failed with exit status 1

In the end, I managed (Reluctantly), by switching to anaconda as base image:

FROM continuumio/anaconda3
WORKDIR /
COPY . .
RUN apt-get update
RUN apt-get -y install libpq-dev gcc
RUN apt-get -y install python3-dev
RUN apt-get -y install postgresql
RUN apt-get -y install gcc
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PATH /opt/conda/bin:$PATH
ENV PYTHONPATH "${PYTHONPATH}:/src"
ENTRYPOINT ["python", "xxx.py"]

Note, these in dockerfile:

RUN apt-get -y install libpq-dev gcc
RUN apt-get -y install python3-dev
RUN apt-get -y install postgresql
RUN apt-get -y install gcc

They are all for sqlalchemy, which depends on psycopg2, which depends on configparser:

configparser
psycopg2
sqlalchemy

It worked. But I really want pypy3.

CodePudding user response:

As far as I know, psycopg2 is not compatible with PyPy. You may want to try out https://pypi.org/project/psycopg2cffi/ instead.

CodePudding user response:

That docker image is using PyPy3.7-v7.3.5, you need at least v7.3.6 in order to use pycopg2. You can file an issue with the docker source repo which is not maintained by the PyPy team. You probably want to use the cffi variant since it should be faster. I would recommend you use the conda-packaged PyPy3.7, it already has binary packages for psycopy2 and many other packages, for linux and windows

conda create -n pypy37 pypy
  • Related