1

Trying to learn Django. Followed the tutorial and created my virtual environment. Now I'm trying to install psycopg2. I can install psycopg2-binary, but I've been told for "psycopg2-binary is good for development and testing but it is advised to use psycopg2 for production". So I'd like to figure this out.

In my venv I'm running pip install psycopg2 it starts with this:

Collecting psycopg2
  Using cached psycopg2-2.8.6.tar.gz (383 kB)
Using legacy 'setup.py install' for psycopg2, since package 'wheel' is not installed.
Installing collected packages: psycopg2
    Running setup.py install for psycopg2 ... error

Then gives this error:

ERROR: Command errored out with exit status 1:
     command: /Users/Me/Documents/Learn/PythonDjango/dev/project/venv/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/hf/9bs1zgkx5pj9gt84snhwrq780000gn/T/pip-install-atdy12xg/psycopg2_cd32e59009264d2bbf87b0a9def98b4e/setup.py'"'"'; __file__='"'"'/private/var/folders/hf/9bs1zgkx5pj9gt84snhwrq780000gn/T/pip-install-atdy12xg/psycopg2_cd32e59009264d2bbf87b0a9def98b4e/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 /private/var/folders/hf/9bs1zgkx5pj9gt84snhwrq780000gn/T/pip-record-30vttiiv/install-record.txt --single-version-externally-managed --compile --install-headers /Users/me/Documents/Learn/PythonDjango/dev/project/venv/include/site/python3.9/psycopg2
         cwd: /private/var/folders/hf/9bs1zgkx5pj9gt84snhwrq780000gn/T/pip-install-atdy12xg/psycopg2_cd32e59009264d2bbf87b0a9def98b4e/

Then it's a long list of generated warnings. I've done some research and I've been unable to get it to work. Running Python 3.9.0 on macOS Big Sur 11.2.1

Dunes
  • 37,291
  • 7
  • 81
  • 97
Kllicks
  • 101
  • 1
  • 9
  • How familiar are you with compiling C libraries from source? This is what pip+psycopg2 is doing behind the scenes. You need to setup your system so that it provides a C compiler, the python header files and the libpq header files, and a program called pg_config. https://www.psycopg.org/docs/install.html#build-prerequisites provides more info on what you need to do to build psycopg2 from source (ie. install psycopg2 rather than psycopg2-binary). – Dunes May 10 '21 at 21:52
  • 1
    psycopg2 currently does not support Python 3.9: https://www.psycopg.org/docs/install.html#prerequisites – AlexK May 10 '21 at 21:53
  • You may also be missing some of the prerequisites, listed here: https://www.psycopg.org/docs/install.html#build-prerequisites. There are other threads on SO about installing prerequisites. [This](https://stackoverflow.com/questions/11618898/pg-config-executable-not-found) thread helped me in the past. – AlexK May 10 '21 at 22:08

1 Answers1

3

on Big Sur (MacOS) I was finally able to install this tonight using the following (had the same issues as you):

(install homebrew before this command if you don't have it already)

brew install openssl
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

then finally.......

pip3 install psycopg2
bondra76
  • 99
  • 7