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
I'm trying to write a Python library in C. My code uses #include <Python.h> but VSCode says:
"include errors detected. Please update your includePath. Squiggles are disabled for this translation unit."
What does this mean and how do I go about fixing this? (Sorry if it's obvious, I'm not new to Python but very new to C)
gcc - fatal error: Python.h: No such file or directory - Stack Overflow
Why is Python.h not found on Ubuntu despite having python-dev installed.
How to include <Python.h> in a C++ project in Visual Studio Code?
Cannot open include file: 'Python.h'
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
You need to provide GCC with the include path for the Python.h header. This can be done with the -I flag:
gcc -c -I/usr/include/python2.7 sourcefile.c
However, there is a better way: use pkg-config :
pkg-config --cflags python
This will output the flags that need to be passed to GCC in order to compile applications that use the Python headers and libraries.
When linking, use the output of this command to include the appropriate libraries:
pkg-config --libs python
You could even combine both steps with:
gcc `pkg-config --cflags --libs python` sourcefile.c
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.
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
I have the latest python3-dev python-dev installed, but the header isn't found. I have read that Python.h is no longer inside python-dev but don't know where, how to get it? I have also tried sudo apt install python-dev-is-python3 but it still doesn't work.
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?