How to fix: “fatal error: Python.h: No such file or directory”

How to fix: “fatal error: Python.h: No such file or directory”

Getting the “fatal error: Python.h: No such file or directory” error can be confusing. We mostly get this error while working with Python extensions or integrating C/C++ modules. This error is especially common when trying to fix Python.h error while building C or C++ Python extension modules. Wrong environment configurations, incompatible Python versions, and not having Python development headers are the main reasons why this issue can occur. In this blog, we’ll learn about the best techniques and knowledge to overcome this error on different platforms like Windows, Linux, and macOS.

Table of Contents:

Importance of Python.h and its operation

Before discussing the approach, we need to understand the file that causes this issue. Python.h is a C header file essential for inserting Python workspace in C programs or writing Python C extensions. It contains important declarations to interface C or C++ code with the Python runtime.

When do we encounter this Error?

If we get an error during this compilation, it means that we are compiling a program or installing packages that need the Python C API, like libraries that involve extensions written in C or C++. There are several reasons why this error occurs. Let’s see some main reasons below.

  • Python Development Header Missing: The Python development headers (included with Python.h missing or not installed on your system. This common error occurs on minimal installations of Python.
  • Incorrect Python Versions: You might have multiple versions of the Python files installed, and the one being used does not have the development files installed.
  • Broken or Incomplete Python Installation: Missing required files can be one of the major reasons for your Python installation to be corrupt or incomplete.
  • Incorrect inclusion of Path: The system might fail to locate the correct directory where Python.h is present due to incorrect configuration in the build or compiler settings.
  • Developers often face this issue when trying to compile Python C API components during package installations or while building from source.

Methods to solve the issue of “Fatal error: Python.h: No such file or directory”

You can start with the following steps and resolve this error based on your Python setup and system.

Method 1: Install Python Development Headers

On many systems, the Python development headers are a part of a separate package that must be installed manually. For the systems working on Windows, open the Command Prompt and check if Python is properly configured by running the following command in the terminal.

python --version
pip install --upgrade pip setuptools wheel

To verify if Microsoft C++ Build Tools is installed, run the following command in the command prompt.

cl ---- to combine languages

Some Python packages require additional dependencies, so to fix that issue, we can install them by running the following command in the command prompt.

pip install --upgrade setuptools wheel

For systems working on macOS, run the following command in the terminal.

brew install python

For systems based on Ubuntu/Debian, you can install Python development headers by running the following command in the terminal to install python3-dev.

sudo apt-get update
sudo apt-get install python3-dev

For systems that are based on Red Hat/Fedora/CentOS, you can write the following command in the terminal to install the Python development headers.

sudo yum install python3-devel

In the above steps, we need to ensure that we are installing the right Python versions in which the dev tools are enabled. Without them, you may encounter the C++ Python extension error when building certain Python packages that include native extensions.

Platform-Specific Path Differences for Python.h

A typical cause of frustration when trying to work with the problem of the “Python.h: No such file or directory” error is the fact that the include path to Python.h differs between platforms and even distributions of Python itself. When you manually build or configure paths in a Makefile or build script, you should be aware of the location of Python.h.

  • Linux: Normally in /usr/include/python3.x/ or /usr/include/x86_64-linux-gnu/python3.x/
  • macOS (Homebrew): In macOS, Homebrew may be installed under /opt/homebrew/include/python3.x/ or /usr/local/include/python3.x/
  • Windows (Microsoft Build Tools): In the case of Python, while using the official installer, the header files reside in C:Users<YourUsername>AppDataLocalProgramsPythonPython3xinclude

To locate it automatically on Unix systems, type this in your terminal:

find /usr/include -name "Python.h"

Or run this in your terminal:

python3 -c "from sysconfig import get_paths; print(get_paths()['include'])"

Installation of build-essential (Linux)

When attempting to compile a program that requires native C/C++ libraries (in particular those that involve Python extensions), you need to install the build-essential package on distributions based on Debian (e.g., Ubuntu). The build-essential package provides the building tools like the GNU compiler collection (GCC), make, and others to build such software from its source.

Update Your Package List

Before you always make sure to update the package index of your system so that you do not install the old versions.

sudo apt update

Install build-essential
This package comprises some of the standard compiling tools such as gcc, g++, and make.

sudo apt install build-essential

Install Python Development headers

The Python.h file you are missing is part of python3-dev (or python3.10-dev, etc, depending on which version you have).

sudo apt install python3-dev

(Optional) Version Check Python

Also, a good practice is checking your Python version to make sure which development package you are installing.

python3 --version

Try to Find out whether python.h exists (Optional)

Once it is installed, you should see whether Python.h file is available now.

find /usr/include -name "Python.h"

Rebuild or Reinstall The Package That Failed

If you installed all dependencies, then rerunning the command that generated the error should hopefully work. The compilation must now work fine.

This will assure you that you have the required compilers and libraries to address such errors as “fatal error: Python.h: No such file or directory“. Your system can otherwise not compile any packages that use C header files like Python.h without build-essential. Nevertheless, there is a high chance that this package will always be installed alongside python3-dev or python3-devel to ensure that all dependencies of Python C extensions are met.

Method 2: Check for the Python Environment and Use the Correct Python version

1. If you have Python installed but have various versions, ensure that you’re using the perfect version in your virtual environment.

Check Python Version (for Windows, Linux, Ubuntu, macOS, etc) by going to the command prompt and checking the version of Python by running the following command in the command prompt.

python3 --version
Check for the Python Environment and Use the Correct Python version

2. If your version of Python is not up to date, then you can follow the mentioned methods for different operating systems.

For Windows, 

You can download the latest version of Python by following the steps that are shown below.

  • Visit the website to download the latest version of Python, You can go to the official website and select your operating system as Windows.
  • Your Windows screen will look like this:
download the latest version of Python
  • Download the latest version for Windows. Currently, the latest Python version is 3.13.2.
  • Look for  “Add Python to PATH” before proceeding to install.
  • Click on Install Now and follow the instructions.
  • After installation of the necessary files, your Windows screen will show that the setup is successful.
successfull python setup

For Linux(Ubuntu/Debian)

You can update the package list by running the command below in the terminal.

sudo apt update && sudo apt upgrade

Install the latest version of Python

Run the following command in the terminal to install the latest version of Python.

sudo apt install python3

For macOS

You need to install Homebrew if not already installed by running the following command in the terminal.

/bin/bash -c "$(curl -fsSL https://brew.sh/install.sh)"

Update Python

You can update the Python version by running the following command in the terminal.

brew install python

If you’re working on a virtual environment, ensure Python is activated by running the following command in the terminal.

source your_env/bin/activate

Method 3: Set the Correct Include Path for Python.h

If the installed header is not recognized by the compiler, you would have to explicitly configure it to the correct directory.

Find the Python.h location

You can run the find command to locate the Python.h file as shown below:

find / -name "Python.h" 2>/dev/null

Add the directory to your include path: If you know the directory where Python.h is located, you can set the CFLAGS environment variable to include it

Working with Virtual Environments (venv, conda)

When you work on several Python projects and install them in different virtual environments, it is no longer a good practice. Virtual environments allow you to work without problems such as an absent development header or a different version of Python. Dependency isolation. There are tools such as venv (a built-in), conda (part of Anaconda) that can assist in isolating your dependencies (including native C extensions that depend on Python.h).

To make a virtual environment with venv, type this in bash:

python3 -m venv myenv
source myenv/bin/activate
  • In this environment, install any packages that could be required. In the event of a build error, ensure the Python you have installed in your virtual environment has development headers. On most systems, you still require installing python3-dev globally.
  • In the case of conda, it is simpler still. The compilers and required headers can be added to Conda environments:
conda create -n myenv python=3.11
conda activate myenv
conda install python-dev
  • Another advantage of using virtual environments is that it will leave your system set up in Python untouched and will also prevent a conflict situation where different projects attempt to access/compile the same resources.

Get 100% Hike!

Master Most in Demand Skills Now!

Advanced Troubleshooting

If this error occurs even after following the above methods, you can apply the following advanced techniques for troubleshooting.

  • Check your Compiler’s Configuration: Sometimes, the issue lies in the specific build or configuration of the compiler used. If the build configuration isn’t aligned with your Python version, you may run into issues when attempting to compile Python C API extensions.
  • Checking Environment Variables: Make sure you are using the correct Python.h Python installation package and monitor the variables PYTHON_INCLUDE or PYTHON_HOME.
  • Consider Cross-Platform Issues: If the project is of a cross-platform type, ensure that the paths and configurations are adapted for the operating system you’re targeting.

Additional Tips for Avoiding This Issue in the Future

  • Use Virtual Environments: Avoid such issues by using virtual environments that help in keeping Python versions isolated from the dependencies, minimizing the potential for conflicts.
  • Ensure Proper Building Tools: Using proper building tools is important. Building tools such as gcc, make, and python3-dev should be installed to avoid this error.
  • Keep Python Environment Updated: Make sure that the Python version is up to date, which ensures that all necessary files are available.

Conclusion

As we have seen before, this issue is a little complex. Understanding the problem properly makes troubleshooting easier. Once Python headers are installed, check your environment and confirm the appropriate paths to fix Python.h error and avoid the C++ Python extension error during development and confirm the appropriate paths. By following all the approaches mentioned in this blog, you can resolve the error and continue working on your projects without any hassle. Staying updated with the Python releases and regular environment checks will help you avoid this error in the future.
Further, check out our Python certification course and get ready to excel in your career with our Basic Python interview questions prepared by experts.

 

Dive into the world of Python libraries and tools with the comprehensive articles listed here.-

ctypes Module in Python – Discover how to use the ctypes module in Python by reading this article.

Count List Elements Using For Loop – This article demonstrates how to iterate through a list and count its elements using a for loop.

Python List Count Method – This article provides insights into counting elements in a Python list using the count() method.

Collections Counter in Python – Discover how to count items efficiently using Counter from the collections module in this article.

Matplotlib Subplot in Python – Here, you’ll find a detailed explanation of creating subplots with Matplotlib in Python.

Measure Elapsed Time in Python – Discover how to measure the time taken by code execution in Python through this article.

re.search() vs re.match() – Discover how re.search() and re.match() function differently in Python in this article.

How to fix: “fatal error: Python.h: No such file or directory” – FAQs

Q1. Why am I getting this error?

Your system is missing the Python development headers that are required for compiling.

Q2. What is the purpose of the Python.h file?

The Python.h file provides the necessary headers for C extensions to interface with Python. It carries structures, macros, and definitions for the compilation of Python-related C programs.

Q3. Checking methods to see if Python.h is installed on my system?

Search in the system’s include directories. If missing, you need to install the Python development package.

Q4. Do we need to reinstall Python if the problem remains?

No, you just install the missing development files. Using the appropriate package manager for your operating system can help resolve this error. 

Q5. What if I am using Conda?

Use the command “conda install -c conda-forge python-devel” to install development headers. If that doesn’t work, install them using your system’s package manager.

Q6. How to fix Python.h error?

Install the Python development package (like python3-dev or python3.x-dev) using your system’s package manager to resolve the missing Python.h error.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

EPGC Data Science Artificial Intelligence