Last updated: Apr 9, 2024
Reading timeยท5 min
To solve the "Fatal error: Python.h: No such file or directory", install the
header files and the static library for your version of Python by adding the
python-dev
package system-wide.
error: command 'gcc' failed with exit status 1 fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated.
Open your terminal and run the command that is suitable for your operating system and package manager.
# ๐๏ธ for Debian (Ubuntu) sudo apt-get install python-dev build-essential # python2.x sudo apt-get install python3-dev build-essential # python3.x # ๐๏ธ for Redhat/CentOS sudo yum install python-devel # python2.x sudo yum install python3-devel # python3.x # ๐๏ธ for Fedora sudo dnf install python2-devel sudo dnf install python3-devel # ๐๏ธ for Alpine Linux sudo apk add python2-dev # python2.x sudo apk add python3-dev # python3.x # ๐๏ธ for openSUSE sudo zypper in python-devel # python2.x sudo zypper in python3-devel # python3.x # ๐๏ธ for Cygwin apt-cyg install python-devel # for python2.x apt-cyg install python3-devel # for python3.x
python.h
files directly.If that didn't help, you have to install python3-dev
for your specific version
of Python.
Use the python --version
command to get your version of Python first.
python --version
For example, my Python version is 3.11, so I'd scope the python-dev
package to
Python 3.11
.
# python3.11 sudo apt-get install python3.11-dev build-essential
If your Python version is 3.10, you'd scope the python-dev
package to Python
3.10
.
# python3.10 sudo apt-get install python3.10-dev build-essential
If your Python version is 3.8, you'd install python3.8-dev
.
# python3.8 sudo apt-get install python3.8-dev build-essential
The python3-dev
package includes header files and a static library for Python
(v3.X).
Installing the header files and the python-dev
library should resolve the
error without you having to edit python.h
files directly.
If the error is not resolved, run the following command.
sudo find / -iname 'Python.h'
The command will try to locate the Python headers.
The output will look something like this.
/usr/include/python3.7/Python.h /usr/include/python3.8/Python.h /home/borislav/anaconda3/include/python3.7m/Python.h /home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.13_1/include/python3.8/Python.h
Make sure to install the python-dev
files for the specific versions of Python,
e.g. 3.7
and 3.8
in the example.
# ๐๏ธ python3.7 sudo apt-get install python3.7-dev build-essential # ๐๏ธ python3.8 sudo apt-get install python3.8-dev build-essential
The error also occurs when the path to the Python.h
file cannot be found on
your machine.
gcc -o output example_file.c
Make sure you haven't misspelled the name of the file in the command.
For example, if you have a main.c
file with the following contents.
#include <stdio.h> int main() { printf("Hello World!"); return 0; }
Install the locate
command if you don't already have it.
# ๐๏ธ for Debian/Ubuntu sudo apt install mlocate # ๐๏ธ for CentOS/RHEL sudo yum install mlocate
Run the command to find the Python.h
executable.
locate Python.h
The output of the command will contain a path to the Python.h
file, e.g.
/usr/include/python3.11/Python.h
.
If the file is not found, then you haven't installed python-dev
as shown in
the previous subheading.
If the path is /usr/include/python3.11/Python.h
, issue the following command.
gcc -I/usr/include/python3.11 main.c
Notice that we aren't specifying the path to the Python.h
file, but to the
directory that contains it.
The command assumes that:
The directory in which your Python.h
file is /usr/include/python3.11
.
The file you are working with is called main.c
.
locate Python.h
command.locate Python.h
Python.h
file, not the complete path to the file.gcc -I/usr/include/python3.11 main.c
My Python.h
file is located in the /usr/include/python3.11
directory.
Google for the name of the package you're trying to install and check if your Python version is supported by the package.
For example, if I google "requests pypi" and click on the
pypi.org page, I can see the supported
Python versions in the sidebar on the left, under Meta
> Requires
.
The screenshot shows that the package supports Python 3.7+.
If the package doesn't support the latest version of Python, try running the
pip install
command with the --pre
option.
pip install requests --pre pip3 install requests --pre python -m pip install requests --pre python3 -m pip install requests --pre py -m pip install requests --pre
requests
with the name of the actual package you are trying to install.The --pre
option makes it so pip
includes pre-release and development
versions of the package. By default pip
only finds stable versions.
If that doesn't work, you have to install a Python version that is in the
specified range and then run the pip install <package_name>
command.
You can upgrade your Python version by downloading the installer from the official python.org website and running it.
Make sure to tick the following options if you get prompted:
You can download a specific Python version that is supported by the package if the package doesn't support the latest Python version.
Different versions are available in the "Looking for a specific release" table.
If none of the suggestions helped, try running the pip install
command in
verbose mode.
pip install requests -vvv pip3 install requests -vvv python -m pip install requests -vvv
The -v
option stands for verbose mode and can be used up to 3 times.
When the pip install
command is run in verbose mode, the command shows more
output and how the error occurred.
To solve the "Fatal error: Python.h: No such file or directory":