36

I am trying to compile a Python extension written in C on a CentOS machine. I am getting

error: Python.h: No such file or directory

It works fine on ubuntu, where I have python-dev installed using apt-get.

I tried installing python-devel using

yum install python-devel

but it is already installed. How do I fix this error?

3
  • 1
    open up a terminal and type find / -name Python.h 2>/dev/null does anything come up?
    – pyCthon
    Commented Sep 12, 2012 at 2:28
  • also What version of CentOS are you on?
    – pyCthon
    Commented Sep 12, 2012 at 2:30
  • 3
    I had the same problem on CentOS Linux release 7.1.1503 and yum install python-devel fixed it.
    – Lqueryvg
    Commented Feb 7, 2016 at 17:39

10 Answers 10

43

If you using python3 You could install python34-devel for that

It's available on EPEL Repository and has python34-devel package available

You simply just run this command to get it installed

yum install python34-devel

Hope it's help

*note At this time python34-devel is the most updated version of python 3.4

1
  • 6
    2.7 => python-devel 3.4 => python34-devel 3.6 => python36-devel Commented Jul 21, 2019 at 13:19
15

On my system the Python.h header file is in the path /usr/include/python2.6/. As this path is not searched by the pre-processor by default, you have to add it to the list of paths to search. This is done with the -I option to the compiler, like this:

$ gcc -I/usr/include/python2.6 source.c -o program

Change the path above to the actual path on your system. You can find it either with the find command as proposed in a comment, of with the locate command if it's installed.

2
  • 1
    ah, sorry, I see, it's the c source file, sorry
    – ehacinom
    Commented Sep 25, 2014 at 3:14
  • 1
    -1. Generally I do not recommend hardcoding any external flags for compilaton. See pkg-config tool that helps resolving some compile-time flags, like: pkg-config python --cflags .
    – Kuchara
    Commented Jun 14, 2023 at 17:48
14
  • for CentOS 7.4
    • Python 3.6.6 : yum -y install python36-devel
    • Python 2 : yum -y install python-devel
  • for Ubuntu
    • Python 3.6: apt-get install libpython3.6-dev
2
  • 7
    yum -y install python3-devel will install the latest available python3 version for that version of CentOS.
    – Matt
    Commented Jan 10, 2020 at 18:15
  • 1
    I was able to resolve the issue and build my docker image by yum -y install python3-devel. Previously I installed python3-dev and I was able to build my image. Is there a name change recently?
    – Pan
    Commented Dec 6, 2021 at 21:27
2

For centos 8+

dnf install -y python3-devel
1

If your are running Centos 8 , you can use :

dnf install -y python38-devel

dnf install -y python3-devel did not work for me.

0

I had a similar problem where default Python was 2.7 but pip was running against 3.4, and I wanted to use 2.7. So I did: yum remove python34-pip and yum install python2-pip. Solved it!

0

I too had similar problem.

./python/py_defines.h:39:20: fatal error: Python.h: No such file or directory

Whereas the find command showed that the python.h exists.

The above solutions did not work however the below command execution worked for me.

yum install python27-python-devel.x86_64

Not much different but wasted lots of time.

0

Install python3.4 (include pip) & python3.4-dev packages on Centos6.X:

yum install http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/python34u-3.4.2-1.ius.centos6.x86_64.rpm
yum install http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/python34u-devel-3.4.2-1.ius.centos6.x86_64.rpm
0

This depends on the Python you have!

You need a dev package, the version X.X.X for Python X.X.X you have, you can search on https://centos.pkgs.org/7/ius-x86_64.

By example, if you have Python 3.6.5 from IUS, you need to install https://centos.pkgs.org/7/ius-x86_64/python36u-devel-3.6.5-1.ius.centos7.x86_64.rpm.html.

0

I had the same problem with Python3.9 on Centos8. I fixed it by installing as below:

dnf install -y python39-devel
1
  • 1
    answer is a replica of another one Commented Jan 10, 2023 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.