'I can't install psycopg2 in my virtual environment

I bought a book Django for professionals and I am currently on 2. chapter PostgreSQL. I have django installed in my virtual environment and my project is in docker as you can see from code below.

When I try to install psycopg I get an error: Warning: Python 3.10 was not found on your system... Neither 'pyenv' nor 'asdf' could be found to install Python. You can specify specific versions of Python with: $ pipenv --python path/to/python.

I run command: docker-compose exec web pipenv install psycopg2-binary==2.8.5

My Dockerfile is

FROM python:3.9

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system

COPY . /code/

My docker-compose.yml file is

version: "3.9"

services:
  web:
    build: .
    command: python3 /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:11
    environment:
    - "POSTGRES_HOST_AUTH_METHOD=trust"


Solution 1:[1]

Even though I'm a bit late...bought the same book and had the same issue. As @David Maze mentioned exit out of your container docker-compose down, don't be inside your venv and install psycopg2 with pipenv:

pipenv install psycopg2

Afterwards restart your container and everything should be working fine. You have to write it in to your Pipfile, not into your active - running - container.

docker-compose up -d --build

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 finethen