Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python.h: No such file or directory

I recently installed KDevelop 4 for C++ development on my Macbook Pro running Ubuntu 12.04 LTS.

I want to embed Python application in my C++ code. To do that, one needs to include the Python.h header file. So, I did that.

#include <iostream> #include <Python.h>  int main(int argc, char **argv) {     Py_Initialize();     return 0; } 

However, on running, I received the following response from the IDE:

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

However, I found the problem soon enough. I hadn't downloaded the python-dev package. So, I did that. I ran again but the same error was there again. So, I thought it must be an issue with the header file not being included by KDevelop. Thus, I added the relevant folder to the include path and KDevelop immediately recognized that by removing the red underline beneath the second include statement in the code above.

But still, the problem remains. I get the same error. Would appreciate any help or inputs you guys can provide :-)

Thanks a lot.

EDIT: Some details that I missed mentioning earlier are that KDevelop is using cmake for my project. I guess the reason my problem is occurring is because cmake doesn't know the appropriate compiler and linker paths. I would appreciate any help in setting the correct paths for cmake.

like image 309
Shubham Goyal Avatar asked Jun 14 '12 21:06

Shubham Goyal


People also ask

How do I fix python H No such file or directory?

You encounter "Python. h: No such file or directory" error while trying to build a shared library using the file extension of another language ( e.g. 'C' ). If you are trying to build a shared library using the file extension of another language, you need to install the correct development version of Python.

What is python3 Dev?

python-dev is the package that contains the header files for the Python C API, which is used by lxml because it includes Python C extensions for high performance.


2 Answers

In your CMakeLists.txt, try adding the following:

find_package(PythonLibs REQUIRED) include_directories(${PYTHON_INCLUDE_DIRS}) target_link_libraries(<your exe or lib> ${PYTHON_LIBRARIES}) 

For details of the commands, run:

cmake --help-module FindPythonLibs cmake --help-command find_package cmake --help-command include_directories cmake --help-command target_link_libraries 
like image 88
Fraser Avatar answered Oct 02 '22 17:10

Fraser


sudo apt-get install pythonX.X-dev 

For example for 3.8

sudo apt-get install python3.8-dev 

Thank you Cristianjs19 for the comment.

Original Answer:

sudo apt-get install python2.7-dev 

worked for me on a "Python.h: No such file or directory" issue

like image 31
Dimitris Baltas Avatar answered Oct 02 '22 16:10

Dimitris Baltas