Fatal error: Python.h: No such file or directory [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Fatal error: Python.h: No such file or directory

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.

shell
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.

shell
# ๐Ÿ‘‡๏ธ 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

install python3 dev

Installing the header files and the static library for your version of Python should be sufficient as it is not recommended to edit python.h files directly.

# Installing python3-dev for a specific Python version

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.

shell
python --version

get python version

For example, my Python version is 3.11, so I'd scope the python-dev package to Python 3.11.

shell
# python3.11 sudo apt-get install python3.11-dev build-essential

install python dev specific version

If your Python version is 3.10, you'd scope the python-dev package to Python 3.10.

shell
# python3.10 sudo apt-get install python3.10-dev build-essential

If your Python version is 3.8, you'd install python3.8-dev.

shell
# 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.

# Locate your Python header files

If the error is not resolved, run the following command.

shell
sudo find / -iname 'Python.h'

locate your python header files

The command will try to locate the Python headers.

The output will look something like this.

shell
/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.

shell
# ๐Ÿ‘‡๏ธ python3.7 sudo apt-get install python3.7-dev build-essential # ๐Ÿ‘‡๏ธ python3.8 sudo apt-get install python3.8-dev build-essential

# Make sure the specified path is correct

The error also occurs when the path to the Python.h file cannot be found on your machine.

shell
gcc -o output example_file.c

python no such file or directory

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.

main.c
#include <stdio.h> int main() { printf("Hello World!"); return 0; }

Install the locate command if you don't already have it.

shell
# ๐Ÿ‘‡๏ธ for Debian/Ubuntu sudo apt install mlocate # ๐Ÿ‘‡๏ธ for CentOS/RHEL sudo yum install mlocate

Run the command to find the Python.h executable.

shell
locate Python.h

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.

shell
gcc -I/usr/include/python3.11 main.c

find python h file

Notice that we aren't specifying the path to the Python.h file, but to the directory that contains it.

The command assumes that:

  1. The directory in which your Python.h file is /usr/include/python3.11.

  2. The file you are working with is called main.c.

The path will likely be different in your case, so make sure to update the path with the output from the locate Python.h command.
shell
locate Python.h

locate python h

Make sure to specify the path to the directory that contains your Python.h file, not the complete path to the file.
shell
gcc -I/usr/include/python3.11 main.c

find python h file

My Python.h file is located in the /usr/include/python3.11 directory.

# Check if your Python version is supported by the package

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.

supported python versions by package

The screenshot shows that the package supports Python 3.7+.

If your Python version doesn't meet the requirements, the "Fatal error: Python.h: No such file or directory" occurs.

If the package doesn't support the latest version of Python, try running the pip install command with the --pre option.

shell
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
Make sure to replace 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:

  • Install launcher for all users (recommended)
  • Add Python to PATH (this adds Python to your PATH environment variable)

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.

install specific python version

# Try running pip install in verbose mode

If none of the suggestions helped, try running the pip install command in verbose mode.

shell
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.

# Conclusion

To solve the "Fatal error: Python.h: No such file or directory":

  1. Install the header files and the static library for your version of Python.
  2. Make sure your Python version is supported by the package you're trying to install.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev