Install psycopg2 failed on macOS
Asked Answered
M

3

6

I have installed PostgreSQL by brew on macOS:

brew install postgresql

Tried these commands, all failed:

  • pipenv install psycopg2
  • pip install psycopg2

Error:

...
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/adapter_qstring.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_qstring.o
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/microprotocols.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols.o
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/microprotocols_proto.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols_proto.o
  In file included from psycopg/microprotocols_proto.c:27:
  In file included from ./psycopg/psycopg.h:37:
  ./psycopg/config.h:81:13: warning: unused function 'Dprintf' [-Wunused-function]
  static void Dprintf(const char *fmt, ...) {}
              ^
  1 warning generated.
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -DPSYCOPG_VERSION=2.8.4 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120001 -DHAVE_LO64=1 -I/Users/user0/.pyenv/versions/3.8.0/include/python3.8 -I. -I/usr/local/include -I/usr/local/include/postgresql/server -c psycopg/typecast.c -o build/temp.macosx-10.14-x86_64-3.8/psycopg/typecast.o
  clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/user0/.pyenv/versions/3.8.0/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/user0/.pyenv/versions/3.8.0/lib build/temp.macosx-10.14-x86_64-3.8/psycopg/psycopgmodule.o build/temp.macosx-10.14-x86_64-3.8/psycopg/green.o build/temp.macosx-10.14-x86_64-3.8/psycopg/pqpath.o build/temp.macosx-10.14-x86_64-3.8/psycopg/utils.o build/temp.macosx-10.14-x86_64-3.8/psycopg/bytes_format.o build/temp.macosx-10.14-x86_64-3.8/psycopg/libpq_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/win32_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/solaris_support.o build/temp.macosx-10.14-x86_64-3.8/psycopg/connection_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/connection_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/cursor_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/cursor_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/column_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_connection_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_cursor_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/replication_message_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/diagnostics_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/error_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/conninfo_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/lobject_int.o build/temp.macosx-10.14-x86_64-3.8/psycopg/lobject_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/notify_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/xid_type.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_asis.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_binary.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_datetime.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_list.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pboolean.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pdecimal.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pint.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_pfloat.o build/temp.macosx-10.14-x86_64-3.8/psycopg/adapter_qstring.o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols.o build/temp.macosx-10.14-x86_64-3.8/psycopg/microprotocols_proto.o build/temp.macosx-10.14-x86_64-3.8/psycopg/typecast.o -L/usr/local/lib -lpq -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.8/psycopg2/_psycopg.cpython-38-darwin.so
  ld: library not found for -lssl
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  error: command 'clang' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for psycopg2

Also, setting this to shell and then reloading:

export PATH=/usr/local/Cellar/postgresql/12.1/bin:$PATH

doesn't work either.

Magistrate answered 6/12, 2019 at 4:37 Comment(5)
Do you have multiple Python versions ?Unwished
@Unwished Yes, I have installed multiple python versions by pyenv. But using 3.8.0 now. What does it matter?Magistrate
Does this answer your question? error install mysqlclient with pip, library not found for -lsslGodunov
stackoverflow.com/…Godunov
Does this answer your question? pg_config executable not foundMetre
S
7

The best solution to go to in this case is to install the binary package

  pip install psycopg2-binary

After it, you could still try to re-install an older version of the psycopg2 package like this. This should work if you don't need necessarily the last version.

  pip install psycopg2==2.7.5
Silvan answered 12/12, 2019 at 11:22 Comment(3)
It also got error: command 'clang' failed with exit status 1 error.Magistrate
@Magistrate installing the command line tools could help solve the issue, do it like this: xcode-select --install. It would be helpful to see the whole error message. But this thread is also similar: #50940802Silvan
Keep in mind that psycopg2-binary is only recommended for development purposes, and should not be used for production.Muzz
U
14

First thing first, you need to verify that openssl is installed and correctly linked:

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

Now, if using python 3.8 means python executable is bound to python 3.8, you may want to try the following workarounds:

  • Workaround #1: Install specifying the interpreter
python -m pip --no-cache install psycopg2

Just to be sure that the version of pip you're calling is indeed the one which is bound to your Python version.

If it still doesn't work, you might be running into this issue in that case you can downgrade your version of Python and try the above command again

  • Workaround #2: Install from source.

First, you need to download the source code hosted on PyPI and run the commands:

tar -xf psycopg2-2.8.4.tar.gz && cd psycopg2-2.8.4 # extract the file
python setup.py build
sudo python setup.py install
Unwished answered 6/12, 2019 at 12:9 Comment(7)
Both of two methods got error: ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib' error: command 'clang' failed with exit status 1.Magistrate
Did you verify the openssl thing ?Unwished
@Magistrate if the answer helped you, don't forget to mark it as validUnwished
Thanks! After eight hours of error messages, your recommendation to EXPORT finally got psycopg2 to install. Why must every, single, step of python be such a disaster of failure? It's like you need to have the secret handshake to make ANYTHING work. Again. Thanks for that final "mysterious knowledge of the ancients" that got my psycopg2 installed. One more day wasted on python configuration.Cist
The exports where enough for me. Already had openssl installed correctly. Thank you so much!Cumin
the exports solves the issue on macOS big surBraunite
I'm getting those. Any ideas on how to solve? ld: warning: directory not found for option '-L/usr/local/opt/openssl/lib' ld: warning: directory not found for option '-L/usr/local/Cellar/gsl/1.16/lib/' ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command '/usr/bin/clang' failed with exit code 1 [end of output] Furthermore I just realized that I do not have a local/opt/ folder. How to fix that? I'm running a Mac M1Keegan
S
7

The best solution to go to in this case is to install the binary package

  pip install psycopg2-binary

After it, you could still try to re-install an older version of the psycopg2 package like this. This should work if you don't need necessarily the last version.

  pip install psycopg2==2.7.5
Silvan answered 12/12, 2019 at 11:22 Comment(3)
It also got error: command 'clang' failed with exit status 1 error.Magistrate
@Magistrate installing the command line tools could help solve the issue, do it like this: xcode-select --install. It would be helpful to see the whole error message. But this thread is also similar: #50940802Silvan
Keep in mind that psycopg2-binary is only recommended for development purposes, and should not be used for production.Muzz
F
0
  • Try downgrade version of your Python3
  • Try use pip3

I solved this problem with that method

Farthermost answered 7/6, 2022 at 3:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.