4

I installed the psycopg2-binary package using pip install psycopg2-binary. But when how can I use it in Python?

import psycopg2 as pg2
conn = pg2.connect(database = 'dvdrental', user = 'postgres', password = secret)
cur = conn.cursor()
cur.execure('select * from actor')
cur.fetchmany(5)

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-6-c2276cd20949> in <module>()
----> 1 import psycopg2 as pg2
      2 conn = pg2.connect(database = 'dvdrental', user = 'postgres', password = secret)
      3 cur = conn.cursor()
      4 cur.execure('select * from actor')
      5 cur.fetchmany(5)
ModuleNotFoundError: No module named 'psycopg2'
tainangao
  • 121
  • 1
  • 2
  • 9
  • Are you running your scripts with the correct version of python (python2 versus python3) for which pip installed the module? – jjanes Jan 19 '20 at 21:15
  • Hey jjanes, I'm using Python 3. It should be alright – tainangao Jan 20 '20 at 15:49
  • For me `pip` has always installed for python2, and I had to use `pip3` to install for python3. But I suppose that may be a matter of configuration. What is your OS and version and package manager? – jjanes Jan 20 '20 at 16:30
  • I'm using MacOS Mojave, version 10.14.6. What do you mean by the package manager? – tainangao Jan 20 '20 at 19:42
  • The package manager controls how you download and install software packages, and how they are configured to work together. Popular package managers on Linux are apt, yum, zypper, and snap. I don't know what the popular choices on Mac are. – jjanes Jan 20 '20 at 20:42

3 Answers3

2

I'm using Django on a Mac. This is what worked for me.

In my virtual environment I ran:

pip install psycopg2-binary

In settings.py I specified:

DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.postgresql_psycopg2',

The database credentials have been left off. I understand this isn't what the OP was asking about, but hopefully it will be helpful to someone.

MTS
  • 1,845
  • 2
  • 17
  • 18
0

Ubuntu/Debian

pg_config is usually installed by libpq-dev.

More details: https://www.psycopg.org/docs/install.html

Mac OS X

Try using Brew Package manager for macOS(or Linux) https://brew.sh/

brew install postgresql

And if installed Postgres.app

Then update the PATH as

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/14/bin

Version can be checked via Applications->Postgres.app->Show Package Content->Contents->Versions->14

Afterwards do

 pip install psycopg2 
Sumit
  • 874
  • 9
  • 10
-1

First install the dependencies,

sudo apt-get install build-dep python-psycopg2

And after that,

pip install psycopg2 
Saroj Rai
  • 1,419
  • 14
  • 13
  • I received `sudo: apt-get: command not found` message. I don't have the prerequisites for psycopg2, so that's why I installed psycopg2-binary – tainangao Jan 20 '20 at 15:50
  • did you try? pip3 install psycopg2 – Saroj Rai Jan 20 '20 at 15:55
  • 1
    Just tried. Didn't work. My pip is the latest version. ```Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/wq/9xr6_qzd54b93xq2gjmqv6900000gn/T/pip-install-xs78x2zx/psycopg2/ You are using pip version 19.0.3, however version 19.3.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.``` – tainangao Jan 20 '20 at 15:59
  • you have mac or windows or linux ? – Saroj Rai Jan 20 '20 at 16:02
  • I'm using Mac. For your reference, this is what I was looking at: http://initd.org/psycopg/docs/install.html – tainangao Jan 20 '20 at 19:44