ImportError: No module named psycopg2

292,306

Solution 1

Step 1: Install the dependencies

sudo apt-get install build-dep python-psycopg2

Step 2: Run this command in your virtualenv

pip install psycopg2-binary 

Ref: Fernando Munoz

Solution 2

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

Solution 3

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

sudo apt-get install libpq-dev
pip install psycopg2

Solution 4

Try installing

psycopg2-binary

with pip install psycopg2-binary --user

Solution 5

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.

Share:
292,306

Related videos on Youtube

ws_123
Author by

ws_123

Updated on July 08, 2022

Comments

  • ws_123
    ws_123 almost 2 years

    In installation process of OpenERP 6, I want to generate a config file with these commands:

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

    But it always showed the message: ImportError: No module named psycopg2

    When I checked for psycopg2 package, it's already installed. Package python-psycopg2-2.4.5-1.rhel5.x86_64 is already installed to its latest version. Nothing to do. What's wrong with this? My server is CentOS, I've installed Python 2.6.7.

    • John Lyon
      John Lyon over 11 years
      You need to accept some answers on some of your other questions.
    • Keith
      Keith over 11 years
      The package installs for python2.4. If you have manually installed 2.6 then you also have to install all the other packages for 2.6.
    • Taizo Ito
      Taizo Ito over 11 years
      Please execute the following command to confirm the python system path. $ python -c 'import sys; print sys.path'
    • Nilesh
      Nilesh over 11 years
      Will you please try to import psycopg2 on the python console?
    • Nilesh
      Nilesh over 11 years
      If you got the answer then select the answer.
    • hassanzadeh.sd
      hassanzadeh.sd about 4 years
      pip install psycopg2
  • aliteralmind
    aliteralmind almost 10 years
  • JcKelley
    JcKelley over 9 years
    I had to run easy_install psycopg2, but your question led me there!
  • Adam Parkin
    Adam Parkin over 8 years
    I found I also had to sudo apt-get install libpq-dev, as I don't have Postgresql installed on the machine I'm trying to install psycopg2 on.
  • Stephan Schielke
    Stephan Schielke almost 8 years
    On arch linux its the same package-name: pacman -S python-psycopg2
  • alekwisnia
    alekwisnia over 7 years
    For Python3 users: install libpq-dev postgresql
  • Mugoma J. Okomba
    Mugoma J. Okomba over 7 years
    On Kubuntu 16.04 build-dep option doesn't work but sudo apt-get install python-psycopg2 works
  • Harun-Ur-Rashid
    Harun-Ur-Rashid over 6 years
    in ubuntu 16.04 it shows E: Unable to locate package build-dep
  • Arthur
    Arthur over 6 years
    Hmm getting ez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1
  • Bennett Garner
    Bennett Garner over 5 years
    You can't use install and build-dep in the same command. They're both commands for apt-get. Should be sudo apt-get build-dep python-psycopg2
  • webelo
    webelo over 4 years
    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).
  • Brian
    Brian about 4 years
    This is the only answer that worked for me with python 3.7.3 and django 3.0.2
  • Muhammad Inaam Munir
    Muhammad Inaam Munir about 4 years
    what is --user for?
  • ibnɘꟻ
    ibnɘꟻ over 3 years
    @MuhammadInaamMunir --user : Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows. You can check more info using pip install --help.
  • Aramis NSR
    Aramis NSR over 3 years
    thanks this helped instead of other top answers
  • Chad
    Chad about 3 years
    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: psycopg.org/docs/install.html#psycopg-vs-psycopg-binary
  • Iris D
    Iris D about 2 years
    This solved my problem! psycopg2 was clearly installed because i could successfully connect to my db, but when I tried to run a streamlit app that needed the results from the connection, i got the import not found statement. This solved it completely. Thank you!