How do I install psycopg2 for Python 3.x?
Asked Answered
M

4

21

Just started Python a few days ago and I'm using PyCharm to develop a web application with Django. I have libpq-dev python-dev packages already installed, but it's still throwing me the same error:

./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory

which according to Google is the issue that occurs when python-dev package isn't installed. Note that I'm running the install from within the PyCharm interface for a virtualenv that I created for 3.2 and 3.3 (not sure how to run it from the terminal). Installing outside of the virtualenv still throws the same error, even when I install it from the terminal with setup.py. If I run pip install psycopg2 from the terminal, it succeeds, but it installs for Python 2.7. According to their website, they have support for up to Python 3.2.

Margarethe answered 31/12, 2013 at 21:18 Comment(0)
E
21

Just run this using the terminal:

$ sudo apt-get install python3-dev

This way, you could use gcc to build the module you're trying to use.

Enlarge answered 31/12, 2013 at 21:25 Comment(3)
If you read my post carefully, you'd notice that I said that I have libpq-dev and python-dev packages already installed and that it works for Python 2.7, but not for 3.x.Margarethe
On Ubuntu that gives: ImportError: No module named 'ConfigParser'Telecast
In case helpful for anyone else, if you have multiple versions of Python3 you might need to be even more specific. E.g. sudo apt-get install python3.7-devQuadrumanous
L
34

On Ubuntu you just run this:

sudo apt-get install python3-psycopg2
Louisville answered 5/12, 2014 at 12:11 Comment(1)
Unfortunately, this is giving me an old version of psycopg2 when I run it. Either that or it's missing some extras for some reason.Hobbema
E
21

Just run this using the terminal:

$ sudo apt-get install python3-dev

This way, you could use gcc to build the module you're trying to use.

Enlarge answered 31/12, 2013 at 21:25 Comment(3)
If you read my post carefully, you'd notice that I said that I have libpq-dev and python-dev packages already installed and that it works for Python 2.7, but not for 3.x.Margarethe
On Ubuntu that gives: ImportError: No module named 'ConfigParser'Telecast
In case helpful for anyone else, if you have multiple versions of Python3 you might need to be even more specific. E.g. sudo apt-get install python3.7-devQuadrumanous
H
18

Another option that seems to provide a newer version of psycopg2 than the one in the python3-psycopg2 package (at least when I wrote this):

sudo apt-get install pip3
sudo apt-get install libpq-dev
sudo pip3 install psycopg2
Hobbema answered 27/1, 2016 at 9:36 Comment(0)
M
2

For most operating systems, the quickest way to install Psycopg is using the wheel package available on PyPI:

$ pip install psycopg2-binary

Check:

$ pip freeze | grep -i psycopg2
psycopg2-binary==2.9.3

This will install a pre-compiled binary version of the module which does not require the build or runtime prerequisites.

Or:

$ sudo apt-get install libpq-dev
$ pip install psycopg2

Check:

$ pip freeze | grep -i psycopg2
psycopg2==2.9.3

More info about psycopg vs psycopg-binary.

Maureen answered 13/4, 2022 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.