45

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

I have python-dev package installed.

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

(KDevelop is using CMake for my project.)

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Shubham Goyal
  • 1,254
  • 4
  • 19
  • 24
  • http://stackoverflow.com/questions/4097339/missing-python-h-while-trying-to-compile-a-c-extension-module – ddzialak Jun 14 '12 at 21:08
  • @ddzialak - Yes, I had a look at that question before asking this one. The reason that question didn't help me is that my Python.h file is actually present at /usr/include/python2.7. I manually checked it and when I click on Python.h in the editor, it opens the file. – Shubham Goyal Jun 14 '12 at 21:19
  • @ShubhamGoyal can you help me with this ? http://stackoverflow.com/questions/31577539/how-can-i-incorporate-cmake-file-when-building-with-distutils-python – AnOldSoul Jul 23 '15 at 03:41

6 Answers6

77

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
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Yeah, this worked. Actually, I had got it to work before seeing your answer and after a lot of reading up on CMake, but the solution is the same :D – Shubham Goyal Jun 15 '12 at 20:09
  • @ShubhamGoyal Nice one. In fact looking at your CMakeLists.txt made me notice that I forgot a set of braces in my original answer! Fixed now. – Fraser Jun 15 '12 at 21:08
  • @Fraser I have a similar question. Can you please help me with it. http://stackoverflow.com/questions/31577539/how-can-i-incorporate-cmake-file-when-building-with-distutils-python – AnOldSoul Jul 23 '15 at 03:40
  • How about `--ldflags`? – ar2015 Aug 05 '18 at 07:34
  • @Fraser i have multiple `CMakeLists.txt` so where should i change it?? – jatin fl Jul 27 '21 at 06:08
  • `find_package(PythonLibs REQUIRED)` does not work for me, instead, the new doc of CMake suggests using `find_package(Python COMPONENTS Development REQUIRED)` and it works for me – maidamai Apr 07 '22 at 01:48
59
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

Dimitris Baltas
  • 3,115
  • 3
  • 34
  • 26
15

You want to include the following on the compile line:

`python-config --cflags`

and this on the link line:

`python-config --ldflags`
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • I am sorry if I am being a noob here but could you please elaborate on what you mean by the compile line and link line in this case? Since I am using the IDE, I am not compiling and linking on the terminal. – Shubham Goyal Jun 14 '12 at 21:24
  • 1
    Your IDE has settings somewhere for include paths, library paths, and compiler and linker flags. Run the commands above - the first one gives you `-I` directives (include paths) and compiler flags, the second one does the same for linker. Put those into appropriate settings in you IDE. – Nikolai Fetissov Jun 14 '12 at 21:38
  • Hmm...KDevelop is using cmake for my project. But I have never used cmake before and would appreciate some help in setting the paths there. – Shubham Goyal Jun 15 '12 at 00:49
  • Sorry, I'm no help with those. The bestI can is to refer you to http://userbase.kde.org/KDevelop4/Manual/Building_%28compiling%29_projects_with_custom_Makefiles – Nikolai Fetissov Jun 15 '12 at 02:37
  • This helped me on Fedora 27. `python-config` was not installed but Fedora knew about the command and asked if I wanted to install it. After installing it also installed `Python.h`! The packages which solved both needs are `python2-devel` and some RPM-related packages (`python-rpm-macros`, `python2-rpm-macros`, and `python3-rpm-generators`). – wallyk Nov 16 '18 at 04:32
6

Most likely Python.h is not in your build systems' include path. You can find out where your Python.h is by running

dpkg -L python-dev | grep Python.h

This will also verify that the python-dev package actually installed a Python.h.

I don't have a kdevelop here, but most IDEs have a setting somewhere where you can specify the include path used by the build system, and you should be able to add the path where Python.h lies there.

EDIT:

As Nikolai implied, you will also need to add the correct library path for the linking stage. (Output of python-config --ldflags).

niko
  • 1,816
  • 13
  • 13
  • I have confirmed that python-dev package did install the Python.h files. The problem is I don't know where or how to add the correct library paths in cmake. Would appreciate any help on that front. I am a complete beginner in cmake. The first time I heard the term was after posting this question. – Shubham Goyal Jun 15 '12 at 00:54
5

For Linux Ubuntu Putty Users try this:

sudo apt-get update
sudo apt-get install python-dev

then compile it

gcc -o check xyz.c -I/usr/include/python2.7/ -lpython2.7

then run it

./check 
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
chhotu sardar
  • 63
  • 1
  • 1
4

I assume that it is already installed. Find the path with:

find / -iname python.h

and when you have done so, when compiling add

-I python_h_path
Roxy
  • 391
  • 2
  • 14