38

When I try to import psycopg2 it show below log for me:

Traceback (most recent call last):
  File "D:/Desktop/learn/python/webcatch/appserver/testpgsql.py", line 2, in <module>
    import psycopg2
  File "D:/Desktop/learn/python/webcatch/appserver/webcatch/lib/site-packages/psycopg2-2.6.1-py3.5-win32.egg/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'

How can I solve it? My platform is win10 (64) and version is python 3.5

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
sappy
  • 770
  • 2
  • 6
  • 16
  • how did you install `psycopg2`? it didn't successfully install when i used `pip install psycopg2` so it may be specific to how it was installed. – Tadhg McDonald-Jensen Mar 19 '16 at 15:02
  • i follow this [site](http://www.stickpeople.com/projects/python/win-psycopg/) to install it on my computer – sappy Mar 19 '16 at 15:09
  • can you check `import psycopg2` in your python console? and check `sys.path` see whether the module path can be found or not? – Haifeng Zhang Mar 19 '16 at 15:43
  • i just see **_psycopg.cp35-win_amd64.py** and **_psycopg1.py** but don't have **psycopg2._psycopg** – sappy Mar 20 '16 at 00:41

12 Answers12

24

Eureka! I pulled my hair out for 2 days trying to get this to work. Enlightenment came from this SO Question. Simply stated, you probably installed psycopg2 x64 version like I did, not realizing your python version was 32-bit. Unistall your current psycopg2, then:

Download: psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe from HERE, then run the following in a Terminal:

C:\path\to\project> easy_install /path/to/psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe
C:\path\to\project> python manage.py makemigrations
C:\path\to\project> python manage.py migrate

You may also need to (re)create super user with:

C:\path\to\project> python manage.py createsuperuser
Community
  • 1
  • 1
Matthew Weber
  • 889
  • 9
  • 22
  • 1
    Thank you so much. This is what finally solved a day and a half's worth of frustration. I only had to run 'easy_install' with the 32-bit psycopg2 version. I didn't have to run the makemigrations/migrate stuff. Cheers! – Chris Leyva Jun 17 '16 at 03:32
  • 1
    I'm using python 3.6, I just downloaded the .exe from [here](http://www.stickpeople.com/projects/python/win-psycopg/) and installed it the normal widows way... As am using **pip** and not **easy_install** – bmatovu Feb 06 '17 at 15:59
  • 2
    This is not just windows or cpu arch related but it depends also on the python version used to compile the package, even if psycopg2 is compiled with another version of python you could hit this problem. Just compiled with 3.6 and was running on 3.7 and had this problem :/ – Mattia Procopio Apr 16 '19 at 11:56
  • @MattiaProcopio You do have to install the correct version based on what you are using, and it looks like 3.7 is yet to be supported by the stickpeople.com developer. – Matthew Weber Apr 24 '19 at 17:45
23

I had the same problem, solved it in this way:

Reinstall the package psycopg2 using pip (by default installed with python 3)

On Linux:

pip uninstall psycopg2

Confirm with (y) and then:

pip install psycopg2

On Windows I add the prefix ('python -m') to the commands above. I think the problem occurs when you change the version of Python. (Even between minor versions such as Python 3.5 and 3.6).

JoDavid
  • 393
  • 2
  • 8
13

I am using psycopg in an AWS Glue Job, where is harder to follow the instructions listed in the other answers.

What I did is installing psycopg2-binary into a directory and zip up the contents of that directory:

mkdir psycopg2-binary
cd psycopg2-binary
pip install psycopg2-binary -t  .
# in case using python3:
# python3 -m pip install --system psycopg2-binary -t  .
zip -r9 psycopg2.zip *

I then copied psycopg2.zip to an S3 bucket and add it as an extra Python library under "Python library path" in the Glue Spark job.

I then launched the job with the following script to verify if psycopg2 is present (the zip file will be downloaded by Glue into the directory in which the Job script is located)

from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
import sys
import os
import zipfile

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

zip_ref = zipfile.ZipFile('./psycopg2.zip', 'r')
print os.listdir('.')
zip_ref.extractall('/tmp/packages')
zip_ref.close()
sys.path.insert(0, '/tmp/packages')

import psycopg2
print(psycopg2.__version__)

job.commit()
Vzzarr
  • 4,600
  • 2
  • 43
  • 80
11

Download the compiled version of psycopg2 from this link https://github.com/jkehler/awslambda-psycopg2. As psycopg2 is C library for python, which need to be compiled on linux to make it work. The compile instruction also given on that link. Thanks to the https://github.com/jkehler.

Hrushikesh Patel
  • 266
  • 4
  • 12
5

This also happens to me in new Ubuntu 18.04. It is caused by missing one file _psycopg.py in the /usr/local/lib/python3.7/site-packages/psycopg2.

It is fixed by:

  1. remove the old psycopg2 from your machine pip3 uninstall psycopg2.
  2. download new pyscopg2 manually from the official page http://initd.org/psycopg/tarballs/PSYCOPG-2-7/psycopg2-2.7.7.tar.gz
  3. tar xvf psycopg2-2.7.7.tar.gz
  4. python setup.py build
  5. sudo python setup.py install
Saray Chak
  • 69
  • 1
  • 6
  • This worked for me on Ubuntu 18.04, with Python 3.9, after downloading sources psycopg2-2.9.3.tar.gz. `python setup.py build` ran fine, but to install I needed `sudo python3 setup.py install`. – jazcap53 Aug 30 '22 at 21:23
2

I had this happen in Linux using Python 3.7. It is caused by missing one file _psycopg.cpython-37m-x86_64-linux-gnu.so in the /usr/local/lib/python3.7/site-packages/psycopg2. I downloaded _psycopg.cpython-37m-x86_64-linux-gnu.so from https://github.com/jkehler/awslambda-psycopg2/tree/master/psycopg2-3.7, and Copied this file into my anaconda lib.

土豆陈
  • 29
  • 1
1

I had this happen in Linux using Python 2 because I had accidentally had my PYTHONPATH set to Python 3 libraries, and it was trying to load the python3 version of psycopg2. Solution was to unset PYTHONPATH.

sudo
  • 5,604
  • 5
  • 40
  • 78
1

I had the same error on Windows, this worked for me: pip install -U psycopg2

I had an older version installed, must have depreciated

Ryan L
  • 101
  • 1
  • 1
1

For lambda functions on Python 3.7, I ended up using the psycopg2-binary library mentioned in these threads:

https://github.com/jkehler/awslambda-psycopg2/issues/51

Using psycopg2 with Lambda to Update Redshift (Python)

pip3 install psycopg2-binary==2.8.3

Snippet from these links:

I ended up using a different library: psycopg2-binary in my requirement.txt file and it working fine now.

solved it by using psycopg2-binary==2.8.3

Dfranc3373
  • 2,048
  • 4
  • 30
  • 44
0

I came to know that most times the WINDOWS packaging does not go fine with LAMBDA.

I faced same issue while running LAMBDA with WINDOWS installed 3rd party pscyopg2 packaging.

Solution:

step1>
I installed psycopg2 in Linux.
Copied both the directories psycopg2_binary-2.8.2.dist-info and psycopg2 from Linux to windows.

step2>
Along with source *.py, packaged with copied 3rd party dependencies psycopg2 in windows to *.zip file

step3>
Upload the file to LAMBDA - Here it goes, It runs successfully without any error.

Community
  • 1
  • 1
Deep
  • 1
0

Windows 10 with conda environment manager (fresh install of Django, wagtail with PostgreSQL), had the same error. Removed psycopg2

conda remove -n myenv psycopg2

it updated some packages, removed others (it also removed django, wagtail...). Then installed psycopg2 back

conda install -n myenv psycopg2

Tested it, import worked

python
>>> import psycopg2

Installed django, wagtail back. python manage.py migrate now populated PostgreSQL.

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
0

In my case, it was other site-packages that was exposed by installing pgcli, uninstalling pgcli resolved the issue for the time being.

This somehow penetrated virtualenv too.

Heechul Ryu
  • 381
  • 4
  • 6