0

I have a C file that I am trying to integrate Python into, but it returns this error "Python.h: No such file or directory". This is my file:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main(int argl, char **argv)
{

        return 0;
}

I also tried to use cmake, but it gives me the same error, this is my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.18)

project(Test)

add_executable(main main.c)

find_package(PythonLibs REQUIRED)
include_directories(${Python_INCLUDE_DIRS})
target_link_libraries(main ${Python_LIBRARIES})

Output picture from the exec

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Please update question to include the text output instead of an external image that will break and render the question less useful. Text is also searchable. – Allan Wind Sep 03 '22 at 03:15

1 Answers1

0

You didn't tell us what operating system you are using. Debian (and Ubuntu) ships that header file in:

$ apt-file search /Python.h|grep h$
libpython2.7-dbg: /usr/include/python2.7_d/Python.h
libpython2.7-dev: /usr/include/python2.7/Python.h
libpython3.9-dbg: /usr/include/python3.9d/Python.h
libpython3.9-dev: /usr/include/python3.9/Python.h
...

and you would install that with:

$ sudo apt install libpython3.9-dev

You tagged this with c and not cmake, so the answer that you now need to include that directory with -I/usr/include/python3.9. On the Debian the best way to do that is using so you would include that as an argument to when compiling the file:

$ pkg-config -cflags python-3.9
-I/usr/include/python3.9 -I/usr/include/x86_64-linux-gnu/python3.9
Allan Wind
  • 23,068
  • 5
  • 28
  • 38