60

I'm attempting to make a website with a few others for the first time, and have run into a weird error when trying to use Django/Python/VirtualEnv. I've found solutions to this problem for other operating systems, such as Ubuntu, but can't find any good solutions for Mac.

This is the relevant code being run:

virtualenv -p python3 venv
source venv/bin/activate
pip install -r requirements.txt

After running that block, I get the following errors:

AssertionError


Failed building wheel for django-toolbelt Running setup.py bdist_wheel for psycopg2

...

AssertionError


Failed building wheel for psycopg2 Failed to build django-toolbelt psycopg2

I believe I've installed the "django-toolbelt" and "psycopg2", so I'm not sure why it would be failing.

The only difference I can think of is that I did not use the command

sudo apt-get install libpq-dev

as was instructed for Ubuntu usage as I believe that installing postgresql with brew took care of the header.

Thanks for any help or insight!

Taygo0o
  • 601
  • 1
  • 5
  • 3

14 Answers14

66

I had the same problem on Arch linux. I think that it's not an OS dependant problem. Anyway, I fixed this by finding the outdated packages and updating then.

pip uninstall psycopg2
pip list --outdated
pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2

hope this helps...

54

I was also getting same error. Using Python 3.7.3 and pip 19.1.1.

error screen

I used following command.

pip install psycopg2-binary==2.8.3
adiga
  • 28,937
  • 7
  • 45
  • 66
Abhishek Singh
  • 641
  • 5
  • 4
49

For MacOS users

After trying all the above methods (which did not work for me on MacOS 10.14), that one worked :

  • Install openssl with brew install openssl if you don't have it already.
  • add openssl path to LIBRARY_PATH :
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
  • install psycopg2 with pip pip3 install psycopg2
Romain
  • 1,271
  • 11
  • 23
18

I had same problem and this appears to be a Mojave Issue, I was able to resolve with:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
Farzad Farazmand
  • 492
  • 1
  • 6
  • 13
  • Thanks @farzad-farazmand. Helped me to to install `pip install psycopg2==2.5.4` after running you above command on macOS 10.14.6. – Oleg Burov Oct 11 '19 at 22:38
14

For Mac OS X users:

1. First check your postgresql path by running this command in terminal:

pg_config

If this fails lookup how to add pg_config to your path.

2. Next install Xcode Tools by running this command in terminal:

xcode-select --install

If you have both those sorted out now try to install psycopg2 again

hahmed
  • 545
  • 5
  • 16
12

TDLR

If you aren't used to installing Python C-extensions, and psycopg2 isn't a core part of your work, try

pip install psycopg2-binary

Building Locally

psycopg2 is a C-extension, so it requires compilation when being installed by pip. The Build Prerequisites section of the docs explain what must be done to make installation via pip possible. In summary (for psycopg 2.8.5):

  • a C compiler must be installed on the machine
  • the Python header files must be installed
  • the libpq header files must be installed
  • the pg_config program must be installed (it usually comes with the libpq headers) and on $PATH.

With these prerequisites satisfied, pip install psycopg2 ought to succeed.

Installing pre-compiled wheels

Alternatively, pip can install pre-compiled binaries so that compilation (and the associated setup) is not required. They can be installed like this:

pip install psycopg2-binary

The docs note that

The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements.

but I would suggest that psycopg2-binary is often good enough for local development work if you are not using psycopg2 directly, but just as a dependency.

Concluding advice

Read the informative installation documentation, not only to overcome installation issues but also to understand the impact of using the pre-compiled binaries in some scenarios.

snakecharmerb
  • 28,223
  • 10
  • 51
  • 86
9

For MacOS users, this question has the correct solution:

install command line tools if necessary:

xcode-select --install

then

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

John
  • 530
  • 4
  • 14
7

On OS X, I was able to solve this by simply upgrading wheel before installing psycopg2:

pip install --upgrade wheel
Elmer Medez
  • 71
  • 1
  • 3
5

For OSX Sierra users, it seems that an xcode update is the solution: Can't install psycopg2 package through pip install... Is this because of Sierra?

Community
  • 1
  • 1
Adam Starrh
  • 5,137
  • 6
  • 42
  • 74
  • 2
    I tried close to 10 different things (including building postgres from source, linking the libs, etc. to my path, practically reimaging my machine) this was all it was! – henrycjc Jul 08 '17 at 07:53
4

I tried all the above solutions but they did not work for me. What I did was change the psycopg2 version in my requirements.txt file from psycopg2==2.7.4 to psycopg2==2.7.6

codezero11
  • 159
  • 2
  • 6
  • Are you describing what you did but then failed? That would not answer this question and be more of a new question. Or is this an answer, as in "Everything described here failed, but the folowing works."? – Yunnosch Feb 03 '20 at 15:53
  • @Yunnosch, I think they are saying that the proposed solutions didn't work, so they posted what did work for them. That said, they basically have the same solution as Utsav Preet from July 7, 2019. – ChrisMM Feb 03 '20 at 16:23
1

Is your error message complete? the most encountered reason for failing to install psycopg2 on mac from pip is pg_config is not in path. by the way, using macports or fink to install psycopg2 is more recommended way, so you don't have to worry about pg_config, libpq-dev and python-dev.

plus, are using Python 3.5? then upgrage your wheel to > 0.25.0 using pip.

Sniper_3B
  • 125
  • 1
  • 1
  • 8
1

I faced the same issue, but the answers above didn't work for me. So this is what I did in my requirements.txt psycopg2-binary==2.7.6.1 and it worked fine

Utsav Preet
  • 218
  • 3
  • 8
1

I had this issue on several packages, including psycopg2, numpy, and pandas. I simply removed the version from the requirements.txt file, and it worked.

So instead of psycopg2-binary==2.7.6.1 I just had psycopg2-binary.

keirasan
  • 135
  • 4
  • I had these packages in my project and have problem to active pipenv virtual environment after updating to python 3.8, all my attempts failed to activate pipenv virtual environment, but after removing version number after pandas, psycopg2 and psycopg2-binary in requirements.txt – m kiani Oct 13 '20 at 19:05
0

I know you are asking for development environment but if you are deploying on server say, Heroku. Just add below line in the requirements.txt of your project.

django-heroku==0.3.1

As this package itself will install the required packages like psycopg2 on server deployment.So let the server(heroku) should take care of it.