13

I'm following this tutorial on how to extend Python with C\C++ code.

The section named "Building the extension module with GCC for Microsoft Windows" fails for me with the following error:

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

The section named "Building the extension module using Microsoft Visual C++" also fails with a similar error:

fatal error C1083: Cannot open include file: 'Python.h': No such file or directory

What should I do to solve this?

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

3 Answers3

52

For Linux, Ubuntu users to resolve the issue of missing Python.h while compiling, simply run the following command in your terminal to install the development package of python:

In Terminal: sudo apt-get install python-dev

Good luck

Karim
  • 1,526
  • 4
  • 13
  • 16
  • 5
    how do i run it in windows –  Jul 17 '17 at 08:15
  • If you are using windows 10, enable the Linux kernel in windows, follow the instructions answered by Nithin K Anil in the following link https://stackoverflow.com/questions/36352627/how-to-enable-bash-in-windows-10-developer-preview -- You will be able to run all Linux commands in windows, just always open the bash.exe as administrator – Karim Aug 04 '17 at 13:09
  • "karimĆ I've tried that on windows. it's not working. sorry can't upvote – greendino Sep 28 '20 at 03:28
  • I have doubts this is the correct answer for Python3, but already have python3-dev installed.... – Dan Ciborowski - MSFT Jan 11 '21 at 16:38
19
  1. Do you have the python dev files so that you can find Python.h?
  2. Do you have the location of Python.h specified to your compiler? with gcc this is usually done through a -I path to include.

Figuring out which of those is failing will solve your problem.

from the article you linked:

gcc -c hellomodule.c -I/PythonXY/include

gcc -shared hellomodule.o -L/PythonXY/libs -lpythonXY -o hello.dll

They assumed you installed python in the default location c:\pythonXY(Where X is the major version number and Y is the minor version number).(in your case Python26) If you put python somewhere else replace /PythonXY with where ever you installed it.

stonemetal
  • 6,111
  • 23
  • 25
0

The Python official documentation has already made it clear. Check it out here

The header files are typically installed with Python. On Unix, these are located in the directories prefix/include/pythonversion/ and exec_prefix/include/pythonversion/, where prefix and exec_prefix are defined by the corresponding parameters to Python’s configure script and version is '%d.%d' % sys.version_info[:2]. On Windows, the headers are installed in prefix/include, where prefix is the installation directory specified to the installer.

To include the headers, place both directories (if different) on your compiler’s search path for includes. Do not place the parent directories on the search path and then use #include ; this will break on multi-platform builds since the platform independent headers under prefix include the platform specific headers from exec_prefix.

And they have provided a convenient way to get the correct cflags that we should pass to compiler. here

So for example, here is what I got after running the command

root@36fd2072c90a:/# /usr/bin/python3-config --cflags
-I/usr/include/python3.5m -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes

Pass those flags to the compiler, and it will work.