95

When installing process of OpenERP 6, I want to generate a config file with this command,

cd /home/openerp/openerp-server/bin/

./openerp-server.py -s --stop-after-init -c /home/openerp/openerp-server.cfg

But it was always showed a message

ImportError: No module named psycopg2

When I check for psycopg2 package, it's already installed,

Package python-psycopg2-2.4.5-1.rhel5.x86_64 already installed and latest version

Nothing to do

What's wrong with this?

My server is CentOS, I've installed python 2.6.7.

Bhavesh Odedra
  • 9,641
  • 12
  • 29
  • 57
ws_123
  • 3,895
  • 6
  • 19
  • 19

17 Answers17

118

Step 1: Install the dependencies

sudo apt-get install build-dep python-psycopg2

Step 2: Run this command in your virtualenv

pip install psycopg2 

Ref: Fernando Munoz

Bhavesh Odedra
  • 9,641
  • 12
  • 29
  • 57
Tarique
  • 2,639
  • 1
  • 25
  • 22
50

Use psycopg2-binary instead of psycopg2.

pip install psycopg2-binary

Or you will get the warning below:

UserWarning: 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.

Reference: Psycopg 2.7.4 released | Psycopg

Yan QiDong
  • 1,833
  • 1
  • 15
  • 22
  • thanks this helped instead of other top answers – Aramis NSR Feb 06 '21 at 10:39
  • Just FYI - "If you are the maintainer of a published package depending on psycopg2 you shouldn’t use psycopg2-binary as a module dependency" see: https://www.psycopg.org/docs/install.html#psycopg-vs-psycopg-binary – Chad May 11 '21 at 16:25
50

I faced the same issue and resolved it with following commands:

sudo apt-get install libpq-dev
pip install psycopg2
Bhavesh Odedra
  • 9,641
  • 12
  • 29
  • 57
Safvan CK
  • 909
  • 7
  • 16
  • This fixed the error message `You need to install postgresql-server-dev-X.Y ...` that I got after running `sudo apt-get install build-dep python-psycopg2` (even thought I have postgresql installed already). – webelo Sep 13 '19 at 18:51
20

Try installing

psycopg2-binary

with pip install psycopg2-binary --user

kometen
  • 4,305
  • 3
  • 36
  • 39
Y. Yazarel
  • 931
  • 5
  • 10
13

Please try to run the command import psycopg2 on the python console. If you get the error then check the sys.path where the python look for the install module. If the parent directory of the python-psycopg2-2.4.5-1.rhel5.x86_64 is there in the sys.path or not. If its not in the sys.path then run export PYTHONPATH=<parent directory of python-psycopg2-2.4.5-1.rhel5.x86_64> before running the openerp server.

Nilesh
  • 17,950
  • 11
  • 74
  • 119
9

Try with these:

virtualenv -p /usr/bin/python3 test_env
source test_env/bin/activate
pip install psycopg2

run python and try to import if you insist on installing it on your systems python try:

pip3 install psycopg2
Bhavesh Odedra
  • 9,641
  • 12
  • 29
  • 57
Cp Verma
  • 452
  • 5
  • 15
3

You need to install the psycopg2 module.

On CentOS: Make sure Python 2.7+ is installed. If not, follow these instructions: http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

# Python 2.7.6:
$ wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
$ tar xf Python-2.7.6.tar.xz
$ cd Python-2.7.6
$ ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
$ make && make altinstall
$ yum install postgresql-libs

# First get the setup script for Setuptools:
$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

# Then install it for Python 2.7 and/or Python 3.3:
$ python2.7 ez_setup.py

$ easy_install-2.7 psycopg2

Even though this is a CentOS question, here are the instructions for Ubuntu:

$ sudo apt-get install python3-pip python-distribute python-dev
$ easy_install psycopg2

Cite: http://initd.org/psycopg/install/

scarver2
  • 7,589
  • 2
  • 50
  • 58
  • Hmm getting `ez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1` – Arthur Nov 08 '17 at 04:54
3

Recently faced this issue on my production server. I had installed pyscopg2 using

sudo pip install psycopg2

It worked beautifully on my local, but had me for a run on my ec2 server.

sudo python -m pip install psycopg2

The above command worked for me there. Posting here just in case it would help someone in future.

Aquarius
  • 351
  • 3
  • 10
3
sudo pip install psycopg2-binary
Surya Bista
  • 321
  • 2
  • 7
2

i have the same problem, but this piece of snippet alone solved my problem.

pip install psycopg2
yts61
  • 407
  • 5
  • 15
2

For python3 on ubuntu, this worked for me:

$sudo apt-get update
$sudo apt-get install libpq-dev
$sudo pip3 install psycopg2-binary
Nicole Douglas
  • 401
  • 2
  • 9
1

For Python3

Step 1: Install Dependencies

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

Step 2: Install

pip install psycopg2
1

check correctly if you had ON your virtual env of your peoject, if it's OFF then make it ON. execute following cammands:

workon <your_env_name>
python manage.py runserver

It's working for me

Soft Technoes
  • 780
  • 1
  • 3
  • 15
1

I faced same problem and waste almost a day to resolve this issue . I have done 2 things 1- use python 3.6 instead of 3.8 2- change django 2.2 version(may be working some higher but i change to 2.2)

Now its working fine

KAMRAN
  • 35
  • 6
1

Run into the same issue when i switch to ubuntu from windows 10.. the following worked for me.. this after googling and trying numerous suggestions for 2 hours...

sudo apt-get install libpq-dev, then pip3 install psycopg2

I hope this helps someone who has encountered the same problem especially when switching for windows OS to linux(ubuntu).

1

Ok, none of the answers here mentions this and it took me some time to stumble upon it on some random website. Please use:

pip3 install psycopg2

This is when you are using Python 3.5, you can check your version by running python -v in the terminal.

Husky931
  • 400
  • 3
  • 9
0

Import Error on Mac OS

If psycopg2 is getting installed but you are unable to import it in your .py file then the problem is libpq and its linkages. The overall steps are reproduced below. You can check it step by step to know which is the source of error for you and then you can troubleshoot from there.

  • Check for installation of libpq in your system it may not have been installed or not linked. If not installed then install it using the command brew install libpq. This installs libpq library. As per the documentation

libpq is the C application programmer's interface to PostgreSQL. libpq is a set of library functions that allow client programs to pass queries to the PostgreSQL backend server and to receive the results of these queries.

  • Link libpq using brew link libpq, if this doesn't work then use the command: brew link libpq --force.
  • Also put in your .zshrc file the following
    export PATH="/usr/local/opt/libpq/bin:$PATH". This creates all the necessary linkages for libpq library .
  • Now restart the terminal or use the following command source ~/.zshrc.

    This works even when you are working in conda environment.

    N.B. pip install psycopg2-binaryshould be avoided because as per the developers of the psycopg2 library

The use of the -binary packages in production is discouraged because in the past they proved unreliable in multithread environments. This might have been fixed in more recent versions but I have never managed to reproduce the failure.

riskdoctor
  • 11
  • 2