Python – Error: No module named psycopg2.extensions

djangopostgresqlpsycopg2python

I am trying to set up a PostgreSQL database for my django project, which I believe I have done now thanks to the replies to my last question Problems setting up a postgreSQL database for a django project. I am now trying to run the command 'python manage.py runserver' in Terminal to get my localhost up but when I run the command, I see this response…

Error: No module named psycopg2.extensions

I'm not sure what this means – I have tried to download psycopg2 but can't seem to find a way to download psycopg2 using homebrew. I have tried easy_install, pip install and sudo but all return errors like this…

Downloading http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz
Processing psycopg2-2.4.5.tar.gz
Writing /tmp/easy_install-l7Qi62/psycopg2-2.4.5/setup.cfg
Running psycopg2-2.4.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-l7Qi62/psycopg2-2.4.5/egg-dist-tmp-PBP5Ds
no previously-included directories found matching 'doc/src/_build'
unable to execute gcc-4.0: No such file or directory
error: Setup script exited with error: command 'gcc-4.0' failed with exit status 1

How to fix this?

Best Answer

The first thing to do is to install the dependencies.

sudo apt-get build-dep python-psycopg2
sudo apt install python3-psycopg2 # Python 3

After that go inside your virtualenv and use:

pip install psycopg2-binary

These two commands should solve the problem.