Home >Backend Development >Python Tutorial >How to Fix 'fatal error: Python.h: No such file or directory' When Compiling C Extensions?
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
yum (CentOS, RHEL):
sudo yum install python-devel # for Python 2.x sudo yum install python3-devel # for Python 3.x
dnf (Fedora):
sudo dnf install python2-devel # for Python 2.x sudo dnf install python3-devel # for Python 3.x
zypper (openSUSE):
sudo zypper in python-devel # for Python 2.x sudo zypper in python3-devel # for Python 3.x
apk (Alpine):
sudo apk add python2-dev # for Python 2.x sudo apk add python3-dev # for Python 3.x
apt-cyg (Cygwin):
apt-cyg install python-devel # for Python 2.x apt-cyg install python3-devel # for Python 3.x
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!