62

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

How to compile it, Ubuntu12 x64.

user2957539
  • 698
  • 1
  • 5
  • 7

8 Answers8

88

Python 2:

sudo apt-get install python-dev

Python 3:

sudo apt-get install python3-dev
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Michal
  • 2,078
  • 19
  • 29
40

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
I159
  • 29,741
  • 31
  • 97
  • 132
8

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

erdimeola
  • 844
  • 8
  • 17
3

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

2

On Fedora, Redhat or centos

Python 2:

    sudo yum install python-devel

Python 3:

    sudo yum install python3-devel
fbonawiede
  • 91
  • 11
2

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
Flair
  • 2,609
  • 1
  • 29
  • 41
Sato
  • 21
  • 1
1

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
dlewin
  • 1,673
  • 1
  • 19
  • 37
  • Are you claiming that something changed in 3.8 or just that that part of the command would have to change with any version number change? – combinatorist Dec 07 '22 at 19:36
  • Ironically, your answer is not deterministic since it would be different as new versions are added, but I appreciate your point that the user enters the same thing each time (to get different results, which is the opposite of deterministic). But, it is useful. Maybe the word you're looking for is "generic" or auto-updating? – combinatorist Dec 07 '22 at 19:37
-1

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

sudo apt-get install libpq-dev
L O C O
  • 37
  • 9