psycopg: Python.h: No such file or directory

Question:

I’m compiling psycopg2 and get the following error:
Python.h: No such file or directory

How to compile it, Ubuntu12 x64.

Asked By: user2957539

||

Answers:

Python 2:

sudo apt-get install python-dev

Python 3:

sudo apt-get install python3-dev
Answered By: Michal

if you take a look at PostgreSQL’s faq page ( http://initd.org/psycopg/docs/faq.html ) you’ll see that they recommend installing pythons development package, which is usually called python-dev. You can install via

sudo apt-get install python-dev

Answered By: erdimeola

This is a dependency issue.

I resolved this issue on Ubuntu using apt-get. Substitute it with a package manager appropriate to your system.

For any current Python version:

sudo apt-get install python-dev

For alternative Python version:

sudo apt-get install python<version>-dev

For example 3.5 as alternative:

sudo apt-get install python3.5-dev
Answered By: I159

As mentioned in psycopg documentation http://initd.org/psycopg/docs/install.html

Psycopg is a C wrapper around the libpq PostgreSQL client library. To install it from sources you will need:

  • C compiler
  • Python header files

They are usually installed in a package such as python-dev a message error such: Python.h: no such file or directory indicate that you missed mentioned python headers.

How you can fix it? First of all you need check which python version installed in your virtual envitonment or in system itself if you didnt use virtual environment. You can check your python version by:

python --version 

After it you should install the same python-dev version which installed on your virtual env or system. For example if you use python3.7 you should install

apt-get install python3.7-dev 

Hope my answer will help anyone

While all answers here are correct, they won’t work correctly anyway:

- sudo apt-get install python3-dev 
- sudo apt-get install python3.5-dev
- etc ..

won’t apply when you are using python3.8, python3.9 or future versions

I recommend using a deterministic way instead :

sudo apt install python3-all-dev
Answered By: dlewin

On Fedora, Redhat or centos

Python 2:

    sudo yum install python-devel

Python 3:

    sudo yum install python3-devel
Answered By: fbonawiede

Based on the python version your your pipenv file requires, you need to install the corresponding dev file.

I was getting this error and my default python version was 3.8 but the pipenv file was requiring the Python3.9 version. So I installed the python3.9 dev.

$ sudo apt install python3.9-dev
Answered By: Sato

if none of the above-suggested answers is not working, try this it’s worked for me.

sudo apt-get install libpq-dev
Answered By: L O C O
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.