How to Fix "Fatal Error: Python.h: No Such File or Directory" in Python [Updated]

Nishil Patel

Nishil Patel

Jun 11, 2024

4 min read

Share

How to Fix "Fatal Error: Python.h: No Such File or Directory" in Python [Updated]

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

Table of Contents

1.

Introduction

2.

Understanding the Error

3.

What Causes the Error?

4.

How to Fix the Error?

5.

Additional Notes

6.

FAQs

Introduction

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.

Understanding the Error

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

What Causes the Error?

Here are some notable causes that force your system to throw the error:

  • Missing Python dev and static libraries — these libraries include the header files required by the compiler, like the “python.h”. When this file goes missing, the compiler fails to find the necessary references and information to compile C/C++ code.
  • Wrong path — the compiler may be looking for “python. h” in an incorrect directory or path.
  • Incorrect compiler settings — wrong compiler settings may not point the system to the correct location of the Python dev headers. If the necessary flags are not set to tell the compiler where to find “python.h”, the error gets triggered.
  • Missing Python env variables — env variables (such as CPATH, CPLUS_INCLUDE_PATH, and C_INCLUDE_PATH) direct the system to find required Python modules and headers to interact with C/C++. If these are not set or are set incorrectly, the system will not be able to locate the “python.h” file, even if it exists on your machine.

Create Bug Reports for Devs and Debug with AI

How to Fix the Error?

Here’s how to fix the error for different operating systems:

For Windows Users:

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:

  • Download the required Python version setup file from the official site.
  • Locate the “python.h” file to ensure it’s there.

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.

For macOS Users:

Here’s how it works on Mac machines:

  • Open the terminal and run this command to install the latest Python version with Homebrew:
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:

  • Use the export command to set CPLUS_INCLUDE_PATH and C_INCLUDE_PATH environment variables to the path where “python.h” is located. For example:
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.

Create Visual Bug Reports with Developer Logs

For Linux Users:

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.

Additional Notes

  • If you’re using virtual environments, you might need to install the dev libraries within the venv or virtualenv of your project.
  • For Mac, don’t forget to add your Python version in the export command. The same goes for Linux while installing dev packages.
  • Python development package names might differ slightly across different Linux distributions. It’s important to use the correct dev package corresponding to their Python version (especially on Linux). Keep an eye on this too.

FAQs

Typically, no additional packages are needed on Windows as the python.h file comes with the standard Python installation. However, make sure you have the latest updates and the correct version of Python installed.

Written by

Nishil Patel | CEO & Founder

Follow

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.

Subscribe to our updates

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.

Nothing here!
Show us some love 💖. Try BetterBugs today if you haven’t already. A quick feedback and a rating on our Chrome web store page would be awesome!

Share your experience with the founderhere!

Don’t wait! Start reporting now.