Home > Backend Development > Python Tutorial > How to Fix 'fatal error: Python.h: No such file or directory' When Compiling C Extensions?

How to Fix 'fatal error: Python.h: No such file or directory' When Compiling C Extensions?

Mary-Kate Olsen
Release: 2025-01-01 11:56:10
Original
796 people have browsed it

How to Fix

Compiling a C Extension File with Python

When attempting to build a shared library using a C extension file, the error "fatal error: Python.h: No such file or directory" might arise. This indicates that the header files and static libraries for Python development have not been properly installed.

To resolve this issue, utilize your package manager to install these dependencies on a system-wide basis. Below are the commands for different package managers:

apt (Ubuntu, Debian):

sudo apt-get install python-dev   # for Python 2.x
sudo apt-get install python3-dev  # for Python 3.x
Copy after login

yum (CentOS, RHEL):

sudo yum install python-devel    # for Python 2.x
sudo yum install python3-devel   # for Python 3.x
Copy after login

dnf (Fedora):

sudo dnf install python2-devel  # for Python 2.x
sudo dnf install python3-devel  # for Python 3.x
Copy after login

zypper (openSUSE):

sudo zypper in python-devel   # for Python 2.x
sudo zypper in python3-devel  # for Python 3.x
Copy after login

apk (Alpine):

sudo apk add python2-dev  # for Python 2.x
sudo apk add python3-dev  # for Python 3.x
Copy after login

apt-cyg (Cygwin):

apt-cyg install python-devel   # for Python 2.x
apt-cyg install python3-devel  # for Python 3.x
Copy after login

Important Note: python3-dev/devel does not automatically cover all minor versions of Python 3. For instance, if you are using Python 3.11, you may need to install python3.11-dev / python3.11-devel.

The above is the detailed content of How to Fix 'fatal error: Python.h: No such file or directory' When Compiling C Extensions?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template