Error while installing psycopg2 using pip

I am trying to run docker in django using this command docker build -t myimage . Now the docker file tries to run the RUN pip install -r /app/requirements.txt --no-cache-dir but when ot gets to the Downloading psycopg2-2.9.3.tar.gz (380 kB) section, it throws the error.

NOTE: i do not have psycopg2 in my requirements.txt file only the psycopg2-binary. So how can i just skip that psycopg2 download?

  Downloading pytz-2022.2.1-py2.py3-none-any.whl (500 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 500.6/500.6 kB 2.6 MB/s eta 0:00:00
Collecting psycopg2
  Downloading psycopg2-2.9.3.tar.gz (380 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 380.6/380.6 kB 2.7 MB/s eta 0:00:00
  Preparing metadata ( started
  Preparing metadata ( finished with status 'error'
  error: subprocess-exited-with-error

Try doing the pip install of psycopg2-binary as a separate step before doing the pip install of the requirements file. (I believe that’s how we do it.)

1 Like

if this is what you meant RUN pip install psycopg2-binary --no-cache-dir as a seperate step, i tried it already, but when it starts installing the requirements.txt after the last package there which is validate-email


then it goes ahead and start installing psycopg2 and some other packages that i did not specify in the requirements.txt. I don’t know if you have experienced that before.

And i also wanted to ask: when deploying to AWS, is docker compulsory?

Yes that’s what I meant - I’ll have to dig up that build process to verify what we do. I know we install psycopg2-binary and we don’t encounter any issues with our other packages trying to install psycopg2. But I haven’t looked at that project in about 2 years…

If you’re talking about building an EC2 instance, absolutely not. (I don’t know if there are other AWS services where it would be required - my use of AWS is extremely limited.)

okay thaks

ohh thanks alot, i would really appreciate that. You can even give me a clue let me also do something about it too along side you.

I just tried a minimal requirements.txt file in a simple docker build.


FROM python:3.10.6-buster

# set work directory
WORKDIR /var/app

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /var/app
RUN pip install -r requirements.txt



Output from docker build .:

Sending build context to Docker daemon   16.9kB
Step 1/5 : FROM python:3.10.6-buster
 ---> fe38d8a70a6c
Step 2/5 : WORKDIR /var/app
 ---> Running in 6d28a481c156
Removing intermediate container 6d28a481c156
 ---> 5807640d0938
Step 3/5 : RUN pip install --upgrade pip
 ---> Running in c95daedbbd20
Requirement already satisfied: pip in /usr/local/lib/python3.10/site-packages (22.2.1)
Collecting pip
  Downloading pip-22.2.2-py3-none-any.whl (2.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 11.9 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.2.1
    Uninstalling pip-22.2.1:
      Successfully uninstalled pip-22.2.1
Successfully installed pip-22.2.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system pack
age manager. It is recommended to use a virtual environment instead:
Removing intermediate container c95daedbbd20
 ---> 41a81b248162
Step 4/5 : COPY ./requirements.txt /var/app
 ---> f52003a5adb7
Step 5/5 : RUN pip install -r requirements.txt
 ---> Running in 1df2b2ffc6d9
Collecting Django
  Downloading Django-4.1-py3-none-any.whl (8.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1/8.1 MB 27.2 MB/s eta 0:00:00
Collecting django-crispy-forms
  Downloading django_crispy_forms-1.14.0-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.3/133.3 kB 2.2 MB/s eta 0:00:00
Collecting environs
  Downloading environs-9.5.0-py2.py3-none-any.whl (12 kB)
Collecting psycopg2-binary
  Downloading psycopg2_binary-2.9.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 26.7 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.3/42.3 kB 788.7 kB/s eta 0:00:00
Collecting asgiref<4,>=3.5.2
  Downloading asgiref-3.5.2-py3-none-any.whl (22 kB)
Collecting python-dotenv
  Downloading python_dotenv-0.20.0-py3-none-any.whl (17 kB)
Collecting marshmallow>=3.0.0
  Downloading marshmallow-3.17.0-py3-none-any.whl (48 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 48.3/48.3 kB 865.0 kB/s eta 0:00:00
Collecting packaging>=17.0
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 kB 734.9 kB/s eta 0:00:00
Collecting pyparsing!=3.0.5,>=2.0.2
  Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.3/98.3 kB 2.2 MB/s eta 0:00:00
Installing collected packages: sqlparse, python-dotenv, pyparsing, psycopg2-binary, django-crispy-forms, asgiref, packaging
, Django, marshmallow, environs
Successfully installed Django-4.1 asgiref-3.5.2 django-crispy-forms-1.14.0 environs-9.5.0 marshmallow-3.17.0 packaging-21.3
 psycopg2-binary-2.9.3 pyparsing-3.0.9 python-dotenv-0.20.0 sqlparse-0.4.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system pack
age manager. It is recommended to use a virtual environment instead:
Removing intermediate container 1df2b2ffc6d9
 ---> d6900586ee60
Successfully built d6900586ee60

So there’s nothing in that list specifically trying to load psycopg2.

It might help if you could identify which package is creating this dependency - there might be some way to work around it.

1 Like

Thanks alot, that fixed it. I used pip show package_name and it happened that it was the django-heroku package that needed psycopg2 package too. Thanks for your help, i really do appreciate it once more.