Nishil Patel
Jun 11, 2024
4 min read
The “fatal error: python.h: no such file or directory” error is a compilation error message you might get while using Python and C/C++, together. In this article, learn more on what causes this error and what you can do to fix it for popular operating systems (Windows, macOS, and Linux).
1.
Introduction
2.
Understanding the Error
3.
What Causes the Error?
4.
How to Fix the Error?
5.
Additional Notes
6.
FAQs
You may see the “fatal error: python.h: no such file or directory” error message when:
— trying to install Python packages to interact with compiling C/C++ code
— building C/C++ programs in Python
This article will guide you step-by-step to fix this error for popular operating systems and ensure the compilation process doesn’t halt when you work with C/C++ and Python.
The “python.h” in the message, is a header file that enables the C/C++ compiler to interact with Python code functions and data structures. This file is the entry point of the Python C API.
The “python.h” file may sometimes not be included by default. When your system can’t locate this file (required for Python and C/C++ to work together), it throws the “fatal error: python.h: no such file or directory” message.
Also Read: How to Fix “Error: Pg_config Executable Not Found.” Python Error
Here are some notable causes that force your system to throw the error:
Here’s how to fix the error for different operating systems:
On Windows systems, the “python.h” header file is generally included automatically during the Python installation process, along with the corresponding dev files. There’s no need for the extra “dev” or “devel” command that’s typically necessary while installing Python dev files on other operating systems—such as Linux—to include this file in your Python directory.
Here’s how to install Python on Windows and check for the “python.h” file:
For example: If you have installed Python 3.12 in the default location, check for the file at the location:
C:\Users\<username>\AppData\Local\Programs\Python\Python312\include
Note: If you have an existing Python version in your system, navigate to the “include” folder just like above, to check for the file.
Try recompiling your C/C++ code. The error should be gone by now.
Here’s how it works on Mac machines:
brew install python
A proper installation should resolve the issue. However, if you are still stuck, try the following to set the env variables manually. Here’s how:
export
CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.12/Headers
export
C_INCLUDE_PATH=/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.12/Headers
In the “export” command, replace the Python version if you have an existing version in your system. This should resolve the error on Mac.
Here are the commands to install Python dev packages for popular Linux distributions to resolve the “fatal error: python.h: no such file or directory” error:
Ubuntu/Debian (using apt):
sudo apt install python3.x-dev # For Python 3.x
sudo apt install python2.x-dev # For Python 2.x
Replace the “3.x” and “2.x” with the desired Python version. For instance, here’s the command for running the command for Python 3.12:
sudo apt install python3.12-dev # For Python 3.12
CentOS/RHEL (using yum):
sudo yum install python-devel # For Python 2.x
sudo yum install python3-devel # For Python 3.x
Fedora (using dnf):
sudo dnf install python-devel # For Python 2.x
sudo dnf install python3-devel # For Python 3.x
openSUSE (using zypper):
sudo zypper in python-devel # For Python 2.x
sudo zypper in python3-devel # For Python 3.x
After installing the necessary libraries or adjusting the include path, recompile your code. The compiler should now successfully locate “python.h” and allow the compilation to proceed.
Nishil is a successful serial entrepreneur. He has more than a decade of experience in the software industry. He advocates for a culture of excellence in every software product.
Meet the Author: Nishil Patel, CEO, and Co-founder of BetterBugs. With a passion for innovation and a mission to improve software quality.
We never spam.
Share your experience with the founderhere!