5

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'
5
  • Are you running your scripts with the correct version of python (python2 versus python3) for which pip installed the module? Commented Jan 19, 2020 at 21:15
  • Hey jjanes, I'm using Python 3. It should be alright Commented Jan 20, 2020 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? Commented Jan 20, 2020 at 16:30
  • I'm using MacOS Mojave, version 10.14.6. What do you mean by the package manager? Commented Jan 20, 2020 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. Commented Jan 20, 2020 at 20:42

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 

1 Comment

That worked for me. Though after installing postgresql I bumped into the next error which looked like this: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun. This was further resolved by running xcode-select --install
-1

First install the dependencies,

sudo apt-get install build-dep python-psycopg2

And after that,

pip install psycopg2 

5 Comments

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
did you try? pip3 install psycopg2
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.
you have mac or windows or linux ?
I'm using Mac. For your reference, this is what I was looking at: initd.org/psycopg/docs/install.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.