120

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Jess
  • 1,749
  • 4
  • 16
  • 17
  • You don't have a compiler installed, and you need one to build the psycopg2 package. If you can provide details about your operating system, people here can probably help you with that. – Ian Clelland Oct 04 '12 at 15:37
  • @IanClelland Thanks Ian. I'm on Mac OS X 10.6.8. I have homebrew installed if that helps. – Jess Oct 05 '12 at 16:04
  • Check what you are using interpreter from the right venv. – Alex78191 Dec 18 '19 at 04:45

21 Answers21

202

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.

karel
  • 5,489
  • 46
  • 45
  • 50
Fernando Munoz
  • 4,541
  • 2
  • 18
  • 16
  • 13
    For Python 3, `sudo apt-get build-dep python3-psycopg2` – laike9m Feb 24 '14 at 15:26
  • 9
    What's the point of building the dependencies with apt, then installing with pip? What's wrong with the single command `sudo apt-get install python-psycopg2`? It worked fine for me. – kdbanman Jun 18 '15 at 16:41
  • 4
    got `You must put some 'source' URIs in your sources.list` while triyng any of the above commands – Revolucion for Monica Jul 02 '18 at 16:34
  • I'm new to python. Where shall I execute the: sudo apt-get build-dep python-psycopg2 – nba2020 Oct 28 '19 at 16:03
  • 2
    @nba2020: sudo apt-get...? These are linux (debian/mint/ubuntu) prerequisities for psycopg2-binary. If you use windows or such something you should find windows prerequisities for psycopg2-binary instead and apply them before installing psycopg2-binary. Or maybe (I'm not sure) you can skip prerequisities and install psycopg2 instead of psycopg2-binary and it could work. However psycopg2 (without -binary) is deprecated so it will be better if you try install psycopg2-binary correctly. – mirek Feb 03 '20 at 17:55
  • This answer is misleading, since the error asked about is actually solved by the pip command, not the apt-get install one. – eMarine Apr 01 '20 at 16:45
  • 1
    This only worked for me, when I ran first command also inside virtualenv – Oto Shavadze Oct 05 '20 at 20:24
  • it worked for me with the command: sudo apt install python3-psycopg2 # Python 3 without installing it in my venv... but why? – Riolite Sep 15 '22 at 21:30
43
pip install psycopg2-binary

The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: http://initd.org/psycopg/docs/install.html#binary-install-from-pypi.

Charlie 木匠
  • 2,234
  • 19
  • 19
  • 1
    This doesn't help me enough. Somewhere in the history I have installed psycopg2 and later (from: -r requirements.txt) properly psycopg2-binary. Everything worked "fine". But once I uninstalled psycopg2 (without -binary) I received the "No module named psycopg2.extensions" error. Uninstall/install psycopg2-binary doesn't help me. For me the prererequisities were not installed (see @FernandoMunoz answer: apt install build-dep python-psycopg2). Installing these Debian packages and later(!) psycopg2-binary has helped me. – mirek Feb 03 '20 at 18:01
10

For Django 2 and python 3 install psycopg2 using pip3 :

pip3 install psycopg2
Ghasem
  • 14,455
  • 21
  • 138
  • 171
6

I installed it successfully using these commands:

sudo apt-get install libpq-dev python-dev
pip install psycopg2
Hassan Ketabi
  • 2,924
  • 2
  • 22
  • 31
6

For macOS Mojave just run pip install psycopg2-binary. Works fine for me, python version -> Python 3.7.2

Kanke
  • 2,527
  • 16
  • 10
4

first install apt-get install python-setuptools

then try easy_install psycopg2

user1667633
  • 4,449
  • 2
  • 16
  • 21
4

This is what helped me on Ubuntu if your python installed from Ubuntu installer. I did this after unsuccessfully trying 'apt-get install' and 'pip install':

In terminal:

sudo synaptic

then in synaptic searchfield write

psycopg2

choose

python-psycopg2

mark it for installation using mouse right-click and push 'apply'. Of course, if you don't have installed synaptic, then first do:

sudo apt-get install synaptic
arunas_t
  • 1,576
  • 1
  • 10
  • 14
3

I ran into this same issues recently and this code worked.

sudo apt-get install libpq-dev python-dev-is-python3

Then

pip3 install psycopg2
Smart D
  • 131
  • 1
  • 3
2

In python 3.4, while in a virtual environment, make sure you have the build dependencies first:

sudo apt-get build-dep python3-psycopg2

Then install it:

pip install psycopg2 
Ghasem
  • 14,455
  • 21
  • 138
  • 171
JJSanDiego
  • 721
  • 5
  • 11
2

On Alpine Linux (majority of the docker containers) do:

apk add postgresql-dev

Then:

pip install psycopg2-binary
1

I used the extension after only importing psycopg2:

import psycopg2

...

psycopg2.extensions.AsIs(anap[i])
f p
  • 3,165
  • 1
  • 27
  • 35
  • @f p Thanks for your reply. How did you import psycopg2? Did you use homebrew? I've tried running the command 'import psycopg2' in my Terminal but it returns 'ImportError: No module named psycopg2'. – Jess Oct 04 '12 at 13:25
  • I'm using ubuntu where it's ready to install without compiling.. The example shown is in python. To install in windows look at http://stickpeople.com/projects/python/win-psycopg/ What you want is to install it first. – f p Oct 04 '12 at 13:31
  • @f p Thank you. I'm on mac - is there a similar solution for mac? – Jess Oct 05 '12 at 16:54
1

It seems that you need gcc-4.0, and it would be helpful to specify your OS type and version.

Maybe this question will help you a bit: Installing GCC to Mac OS X Leopard without installing Xcode

Update

I'm a Windows user, so I can't test your setup, but a quick google pointed to some more links:

Community
  • 1
  • 1
alexandrul
  • 12,856
  • 13
  • 72
  • 99
  • Thank you. I've downloaded and installed GCC 10.6 as my OS is 10.6.8. I then ran easy_install psycopg2 but it returned this error... "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". Do you know why that might be? – Jess Oct 05 '12 at 17:37
1

I encountered the No module named psycopg2.extensions error when trying to run pip2 install psycopg2 on a Mac running Mavericks (10.9). I don't think my stack trace included a message about gcc, and it also included a hint:

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

I looked for the pg_config file in my Postgres install and added the folder containing it to my path: /Applications/Postgres.app/Contents/Versions/9.4/bin. Your path may be different, especially if you have a different version of Postgres installed - I would just poke around until you find the bin/ folder. After doing this, the installation worked.

Galen Long
  • 3,693
  • 1
  • 25
  • 37
1

try this:
sudo pip install -i https://testpypi.python.org/pypi psycopg2==2.7b2
.. this is especially helpful if you're running into egg error

on aws ec2 instances if you run into gcc error; try this
1. sudo yum install gcc python-setuptools python-devel postgresql-devel
2. sudo su -
3. sudo pip install psycopg2

1

This one worked for me

python manage.py migrate

shree..
  • 13
  • 5
1

I had such problem when trying to run python script as a sudo, while psycopg2 was installed via pip3 to my own user's directory.

I managed to resolve the issue for myself removing pip3 version, and just installing it via apt:

pip3 uninstall psycopg2
sudo apt install python3-psycopg2
natalka
  • 51
  • 2
1
pip3 install django-psycopg2-extension

I know i am late and there's lot of answers up here which also solves the problem. But today i also faced this problem and none of this helps me. Then i found the above magical command which solves my problem :-P . so i am posting this as it might be case for you too.
Happy coding.

Abhay Kumar
  • 293
  • 2
  • 5
0

you can install gcc for macos from https://github.com/kennethreitz/osx-gcc-installer
after instalation of gcc you'll be able to install psycopg with easy_install or with pip

Ponimas
  • 166
  • 1
  • 3
0

Check if you have installed psycopg2 if not

sudo apt-get install psycopg2

Install the dependencies.

sudo apt-get build-dep python-psycopg2

These two commands should solve the problem.

Jayakumar Bellie
  • 9,298
  • 2
  • 17
  • 14
0

This error raise because you not install postgres database in you project virtutal environment. you should run one of these command. from a terminal you can you command for sudo.

sudo apt-get install build-dep python-psycopg2

for pip (pip basically work for python)

pip install psycopg2

or

pip3 install psycopg2-binary

i'm pretty sure it will work for you.

0

Below commands worked for me on Mac OS

   brew install libpq
   pip install psycopg2-binary
Mansi Shah
  • 227
  • 2
  • 6