You need to qualify your include

#include <python2.7/Python.h>

Or tell gcc where to find Python.h with the

gcc -I /usr/include/python2.7/ program.c 
Answer from squareborg on askubuntu.com
🌐
Python
docs.python.org › 3 › c-api › intro.html
Introduction — Python 3.14.7 documentation
Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included. It is recommended to always define PY_SSIZE_T_CLEAN before including Python.h.
Discussions

gcc - fatal error: Python.h: No such file or directory - Stack Overflow
By clicking “Sign up”, you ... you have read our privacy policy. ... Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools. Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content. Stack Ads Connect your brand to the world’s most trusted technologist communities. Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... pythonjavascriptc#reactjsja... More on stackoverflow.com
🌐 stackoverflow.com
Why is Python.h not found on Ubuntu despite having python-dev installed.
Did you try libpython3-dev? I think that might be the one with the C header file More on reddit.com
🌐 r/C_Programming
11
0
December 13, 2024
How to include <Python.h> in a C++ project in Visual Studio Code?
just some thoughts: "Python.h" "python.h" "/somewhere/else/python.h" More on reddit.com
🌐 r/learnprogramming
3
2
June 3, 2022
Cannot open include file: 'Python.h'
P.S. I have included in my test project the requested path: C:\Program Files\Python39\include But now I got another missing files, which make me to think that I am not going in the right direction to solve this. More on github.com
🌐 github.com
5
September 8, 2021
🌐
GitHub
github.com › python › cpython › blob › main › Include › Python.h
cpython/Include/Python.h at main · python/cpython
// C extensions should only #include <Python.h>, and not include directly · // the other Python header files included by <Python.h>. · #ifndef Py_PYTHON_H · #define Py_PYTHON_H · · // Since this is a "meta-include" file, "#ifdef __cplusplus / extern "C" {" // is not needed.
Author: python
Top answer
1 of 16
3620

Looks like you haven't properly installed the header files and static libraries for python dev. Use your package manager to install them system-wide.

For apt (Ubuntu, Debian...):

sudo apt install python-dev   # for python2.x installs
sudo apt install python3-dev  # for python3.x installs

For yum (CentOS, RHEL...):

sudo yum install python-devel    # for python2.x installs
sudo yum install python3-devel   # for python3.x installs

For dnf (Fedora...):

sudo dnf install python2-devel  # for python2.x installs
sudo dnf install python3-devel  # for python3.x installs

For zypper (openSUSE...):

sudo zypper in python-devel   # for python2.x installs
sudo zypper in python3-devel  # for python3.x installs

For apk (Alpine...):

# This is a departure from the normal Alpine naming
# scheme, which uses py2- and py3- prefixes
sudo apk add python2-dev  # for python2.x installs
sudo apk add python3-dev  # for python3.x installs

For apt-cyg (Cygwin...):

apt-cyg install python-devel   # for python2.x installs
apt-cyg install python3-devel  # for python3.x installs

Important Note: python3-dev/devel does not automatically cover all minor versions of python3.
E.g If you are using python 3.11 you may need to install python3.11-dev / python3.11-devel.

2 of 16
581

On Ubuntu, I was running Python 3 and I had to install

sudo apt-get install python3-dev

If you want to use a version of Python that is not linked to python3, install the associated python3.x-dev package. For example:

sudo apt-get install python3.5-dev
🌐
Tutorialspoint
tutorialspoint.com › python › python_further_extensions.htm
Python - Further Extensions
An initialization function. You need to include Python.h header file in your C source file, which gives you the access to the internal Python API used to hook your module into the interpreter.
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.7 documentation
To support extensions, the Python ... to most aspects of the Python run-time system. The Python API is incorporated in a C source file by including the header "Python.h"....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-fatal-error-python-h-no-such-file-or-directory
How to fix: "fatal error: Python.h: No such file or directory" - GeeksforGeeks
July 23, 2025 - The error message "fatal error: Python.h: No such file or directory" typically occurs when the C/C++ compiler is unable to find the Python.h header file, which is part of the Python development package required for compiling C/C++ code that interacts with Python.
🌐
AskPython
askpython.com › python › examples › fix-gcc-python-h-no-such-file
[Fix] gcc - fatal error: Python.h: No such file or directory - AskPython
April 10, 2025 - These files provide access to Python’s C API and definitions needed to build Python-aware extensions. The Python.h file is the major header that gives access to core Python data structures and functions.
🌐
BetterBugs
betterbugs.io › blog › how-to-fix-fatal-error-python-h-no-such-file-or-directory
How to Fix “Fatal Error: Python.h: No Such File or Directory” in Python [Updated]
June 10, 2024 - This file is the entry point of the Python C API. The “python.h” file may sometimes not be included by default.
🌐
Python
svn.python.org › projects › python › branches › pep-0384 › Include › Python.h
Python.h
#ifndef Py_PYTHON_H #define Py_PYTHON_H /* Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" { */ /* Include nearly all Python header files */ #include "patchlevel.h" #include "pyconfig.h" #include "pymacconfig.h" #include
🌐
Reddit
reddit.com › r/learnprogramming › how to include in a c++ project in visual studio code?
r/learnprogramming on Reddit: How to include <Python.h> in a C++ project in Visual Studio Code?
June 3, 2022 -

I want to create a multi-language project in Visual Studio Code using C++ and Python and I need to call a .py file from my CPP code. I did some research and my CPP code ended up looking like this:

#include <iostream>
include <string>
include <Python.h> //after attempting to compile, an error appears here.

int main() 
{ 
   FILE* pf = fopen("test.py", "r"); 
   std::string path = "...";

   Py_Initialize(); 
   PyRun_SimpleFile(pf, path.c_str()); 
   Py_Finalize(); 
}

I have added Python310\include to includePath in settings.json as this:

"C_Cpp.default.includePath": [
        "...\\Python\\Python310\\include"
 ]

but when I try to compile this code with G++ it returns:

fatal error: Python.h: No such file or directory

I have searched this error but there are no posts of this happening with Visual Studio Code. How can I fix this using VSC on Windows 11?

🌐
Jayrambhia
jayrambhia.com › blog › pythonc-api-1
Python/C API: #include "Python.h"
December 9, 2012 - To understand basics, I went through the reference manual on Python website but it was all too complicated and direct. So I directly opened one of the C++ bindings of mahotas and started understanding it. But still went through few topics of Python/C API. To summarize, All function, type and macro definitions are included in Python.h.
🌐
Wikibooks
en.wikibooks.org › wiki › Python_Programming › Extending_with_C
Python Programming/Extending with C - Wikibooks, open books for an open world
The above produces files hello.py and hello_wrap.c. The next step is compiling; substitute /usr/include/python2.4/ with the correct path to Python.h for your setup:
🌐
GitHub
github.com › Yikun › Python3 › blob › master › Include › Python.h
Python3/Include/Python.h at master · Yikun/Python3
#define Py_PYTHON_H · /* Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" { */ · /* Include nearly all Python header files */ · #include "patchlevel.h" #include "pyconfig.h" #include "pymacconfig.h" · #include <limits.h> · #ifndef UCHAR_MAX ·
Author: Yikun
🌐
GitHub
github.com › lava › matplotlib-cpp › issues › 282
Cannot open include file: 'Python.h' · Issue #282 · lava/matplotlib-cpp
September 8, 2021 - 1>c:\project\matplotlib-cpp\matplotlibcpp.h(5): fatal error C1083: Cannot open include file: 'Python.h': No such file or directory Yes, I know I should use libpython library (I guess), but until there I need to overcome this error: Cannot open include file: 'Python.h'
Author: lava
🌐
Jam
jam.dev › blog › python-fatal-error-python-h-no-such-file-or-directory
Fixing Fatal Error: python.h: No Such File or Directory | Easy Solutions
October 3, 2024 - Typically, this error occurs when the C compiler fails to locate the necessary python.h header file.
🌐
Python
svn.python.org › projects › python › tags › r271 › Include › Python.h
Python
#ifndef Py_PYTHON_H #define Py_PYTHON_H /* Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" { */ /* Include nearly all Python header files */ #include "patchlevel.h" #include "pyconfig.h" #include "pymacconfig.h" /* Cyclic gc is always enabled, starting with release 2.3a1.