4

I have a problem with installing psycopg2 on my mac. I tried several things such as installing it with pip and also homebrew. I also downloaded all dependencies but it still fails to install it.

I get the following error message, which looks horrifying:

Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/1c/nhs40gy513143dfqyblmt3r80000gn/T/pip-install-rxlPem/psycopg2/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/1c/nhs40gy513143dfqyblmt3r80000gn/T/pip-record-93LksX/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/1c/nhs40gy513143dfqyblmt3r80000gn/T/pip-install-rxlPem/psycopg2/

What could be the issue?

EDIT: Full error can be found here, and this appears to be the most relevant part:

clang: warning: no such sysroot directory: '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.sdk' [-Wmissing-sysroot]
In file included from psycopg/psycopgmodule.c:27:
In file included from ./psycopg/psycopg.h:34:
/Library/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h:25:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Al Nikolaj
  • 305
  • 4
  • 13

3 Answers3

4

psycopg2 has some install dependencies that might be giving you trouble. In this case it looks like the install process can't find stdio.h, part of the C standard library. Installing the Xcode Command Line Tools might help.

But if you don't want to bother with this, try installing psycopg2-binary instead:

You can also obtain a stand-alone package, not requiring a compiler or external libraries, by installing the psycopg2-binary package from PyPI:

$ pip install psycopg2-binary

The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources.

Community
  • 1
  • 1
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • I tried to check the dependencies and I have fulfilled pretty much everything on the list: Python version 2.7 Check! (Pre-Installed on Mac) Python 3 versions from 3.4 to 3.7 Check! (Installed) PostgreSQL server versions from 7.4 to 11 Check! (updated) PostgreSQL client library version from 9.1 Check! (installed) – Al Nikolaj May 02 '19 at 12:48
  • You'll also need Python headers for whichever version you're using (and you [might need to explicitly provide their location](https://stackoverflow.com/a/15931544/354577) and PostgreSQL headers (maybe `libpq`?). But if you were able to install `psycopg2-binary` why do you still want to install `psycopg2`? – ChrisGPT was on strike May 02 '19 at 12:58
  • If you really want to go down that rabbit hole check out [the answers to this question](https://stackoverflow.com/q/33866695/354577). But unless you have a specific need to build `psycopg2` from source you might as well just stick with `psycopg2-binary`. It's a much simpler solution for a development box. – ChrisGPT was on strike May 02 '19 at 13:00
  • 1
    I have tried that solution, also did not work... I posted the FULL error (it is huge) under this link: https://pastebin.com/yL3BzJeG Maybe this helps? – Al Nikolaj May 02 '19 at 13:39
  • I'm still curious why `psycopg2-binary` doesn't meet your needs. Is there a specific reason you must build `psycopg2` from source? In any case, I've updated my answer based on the extended output you provided. – ChrisGPT was on strike May 02 '19 at 14:38
  • To be completely honest, I do not know how to use psycopg2-binary, because import psycopg2 is not working and I do not find any documentation about using binary – Al Nikolaj May 02 '19 at 15:28
  • @AlNikolaj, it's exactly the same thing except it's not built from source. Use `import psycopg2` and away you go. – ChrisGPT was on strike May 02 '19 at 17:57
  • ok, then I have the next issue haha I have sycopg2-binary installed, but when I try to import the package, it says "ImportError: No module named psycopg2" – Al Nikolaj May 02 '19 at 23:12
  • That sounds like a general issue with the way you're installing packages. IMO that's a separate issue. Are you using a virtual environment? Are you installing with `pip`? Poetry? Pipenv? `conda`? Homebrew? – ChrisGPT was on strike May 02 '19 at 23:16
  • I usually use pip, but due to this issue I installed Homebrew since it was recommended. I might just re-install the whole entire OS... – Al Nikolaj May 03 '19 at 06:42
2

for anybody looking for solution or future reference to this,

I faced this issue while installing psycopg2 on Mojave, following on, i could find link to psycopg2 github issue. which has the solution as installing missing library header, using

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

This is probably an issue with Mojave, enter link description here

Ansuman
  • 428
  • 1
  • 4
  • 15
0

Anyone having this issue with a Debian distribution can use this solution

Install the development headers for postgresql:

sudo apt install python3-dev libpq-dev

then try:

pip3 install psycopg2

hope this solves your issues

Anish Gowda
  • 71
  • 10