Jonnny
Jonnny

Reputation: 5039

No module named 'psycopg2'

I Installed psycopg2 using pip

C:\Users\username>python -m pip install psycopg2

Adjusted my settings to:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2', # also tried: django.db.backends.postgresql
    'NAME': 'name',
    'USER': 'user',
    'PASSWORD': 'my_password',
    'HOST': 'localhost',
    'PORT': '5432',
   }
}

When I attempt to run the server:

import psycopg2 as Database ImportError:

No module named 'psycopg2'

So I ran this again and got:

python -m pip install psycopg2 Requirement already satisfied (use --upgrade to upgrade): psycopg2 in ...

So what have I done wrong? I am new to Django

Upvotes: 7

Views: 29203

Answers (7)

Robert Johnstone
Robert Johnstone

Reputation: 5371

I found that I was unable to import psycopg2 because I had created my virtualenv like this:

$ virtualenv -p python3 .venv

as soon as I change this to:

$ virtualenv .venv

Everything worked!

Upvotes: 0

AKW
AKW

Reputation: 76

On mac-os pip install psycopg2-binary will fix the problem

Upvotes: 3

deathdater
deathdater

Reputation: 1

Install below dependency before installing psycopg2

apt-get install libpq-dev

Now install:

pip install psycopg2

Give the below command to generate DB for your Django config

python manage.py migrate

Upvotes: 0

Hardik Patel
Hardik Patel

Reputation: 155

You can try

pip install psycopg2-binary

Upvotes: 5

rll
rll

Reputation: 5587

It is possible that while following the installation tutorial you installed virtualenv, created some environment (like env1) and followed on to install django on that environment.

If so, than you are running django on that environment as well (otherwise it would not be installed). In order to install modules you need to first activate the environment and then use pip.

Upvotes: 3

Morishiri
Morishiri

Reputation: 884

There is a possibility that you have two versions of python installed and python2-pip is aliased as pip while python3-pip is aliased as pip3.

Make sure you are using right version with python and pip.

So the command would be:

pip3 install psycopg2

Upvotes: 11

ettanany
ettanany

Reputation: 19826

You may need to install the package from whl file, try the below command:

pip install psycopg2-2.6.2-cp27-cp27m-win_amd64.whl

You can download this package from http://www.lfd.uci.edu/~gohlke/pythonlibs/

Upvotes: 4

Related Questions