psycopg: Python.h: No such file or directory
Asked Answered
A

8

66

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

How to compile it, Ubuntu12 x64.

Allaround answered 7/11, 2013 at 18:36 Comment(0)
C
96

Python 2:

sudo apt-get install python-dev

Python 3:

sudo apt-get install python3-dev
Centiliter answered 7/11, 2013 at 18:36 Comment(3)
Same issue for Python3.7. To install run $sudo apt-get install python3.7-devIlliquid
Same issue for Python3.9. To install run $sudo apt-get install python3.9-devExegetics
and for python 3.10 (Ubuntu 20.04): $ sudo apt-get install python3.10-devHolleran
M
44

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
Mcmahan answered 8/9, 2014 at 11:26 Comment(3)
When just "sudo apt-get install python3" wouldn't do, "sudo apt-get install python3.6-dev" solved it for me!Palazzo
my vote for best answer based on added detail for clarity.Margheritamargi
Specifying the version worked for me. Example: sudo apt-get install python3.8-devFat
R
9

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

Relapse answered 7/11, 2013 at 18:39 Comment(0)
H
5

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

Haas answered 16/1, 2020 at 16:39 Comment(0)
A
4

On Fedora, Redhat or centos

Python 2:

    sudo yum install python-devel

Python 3:

    sudo yum install python3-devel
Air answered 7/12, 2021 at 10:11 Comment(0)
O
3

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
Oberstone answered 24/1, 2022 at 6:31 Comment(0)
D
2

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
Deweese answered 23/2, 2021 at 10:1 Comment(2)
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?Macdonell
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?Macdonell
P
1

This may help. please check

sudo apt-get install libpq-dev
Paphlagonia answered 23/11, 2022 at 9:53 Comment(1)
This will not solve the issue in the question.Jezabella

© 2022 - 2024 — McMap. All rights reserved.