20

I have been using Cython to compile my Python files into C files and then use MinGW to create an executable from the C file. Cython works fine, I can type cython test.pyx into the command line and get a C file. The problem is when I attempt to compile an executable from the C file. If I type gcc test.c I get the following error:

test.c:4:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.

I would really appreciate some help. I am running windows 7 and python 3.5.

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47

2 Answers2

36

you probably don't have python-dev installed. Depending on your OS, you'd need to do something like this:

sudo apt-get install python-dev

Which is what you'd do on Ubuntu

bruceg
  • 2,433
  • 1
  • 22
  • 29
  • 15
    btw: For python3 users you need to use `sudo apt-get install python3-dev` – The Bndr Oct 26 '18 at 08:25
  • 4
    @The Bndr didnt neither one of them work for me and I am working on ubuntu 18.04. Already have both dev environments installed. Working out of pycharm everything is the latest version. – idzireit May 08 '19 at 03:54
  • 1
    additional comment: For special python version, say 3.11, could append version number `sudo apt-get install python3.11-dev` – cross-hello Jul 31 '23 at 03:47
5

in gcc

#include "file.h"

tells gcc to find the file in the same directory where test.c is, and

#include <file.h>

means to find file.h in the gcc include paths, which can be added with -I

gcc -I/path/to/the/file_h test.c

you might try

#include <Python.h>

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

Community
  • 1
  • 1
ku'
  • 102
  • 4
  • That gets me past the fatal error: Python.h: No such file or directory. But now I am getting the following error: `collect2.exe: error: ld returned 1 exit status` Any Idea what that means an how to fix it? – Mark Skelton Jan 08 '16 at 13:00
  • 1
    ld returned 1 means that the compile of test.c is successful, but the linker didn't find the code of functions in Python.h. You need to add the library path to let the linker know. Please try add "-lpython2.6" for compiling (like `gcc -o test test.c -lpython2.6`), notice you may have to change the version of python. Also see http://stackoverflow.com/questions/14260196/compile-error-gcc-with-python-h. – ku' Jan 09 '16 at 03:46
  • 1
    I tried the code you gave and that got me back to the original error I was encountering. I don't know much about the C language, I just want to use Cython for creating fast executable scripts. – Mark Skelton Jan 09 '16 at 16:38
  • Could you paste your code, commands and error message on gist or something like that for debugging? – ku' Jan 10 '16 at 05:34
  • I don't have a github account but I can tell you my code with one line. After finding the problem on a longer piece of code. I tried the old `print("hello world")` file. I then ran cython through the command line like this: `cython helloworld.pyx` which gave me a C file. Then I ran gcc like this: `gcc helloworld.c`. I have tried your suggestions and just about everything else I could find on the internet but I still can't figure it out. – Mark Skelton Jan 10 '16 at 13:29
  • Below is what I did: `cython a.pyx` `gcc -c a.c -I/usr/include/python2.7` and successful. By the way, it's no need an account for using [gist](https://gist.github.com/). Very convenient for sharing codes, you can have a try :) . – ku' Jan 11 '16 at 03:03
  • In your case in Windows, try `gcc -c a.c -IC:\Python35\include` instead. – ku' Jan 11 '16 at 03:10
  • Well this time it finally worked but I got a .o file as the output what am I supposed to do with that? – Mark Skelton Jan 11 '16 at 11:59
  • Good to hear this. I did followings to use a.pyx in b.py code. Suppose `def func()` is in a.pyx. `gcc -shared -fPIC -I /usr/include/python2.7 -o a.so a.c`, a.so will be created. Add `import a` in b.py, and you can use `a.func()`. – ku' Jan 12 '16 at 06:05
  • Try `gcc -shared -fPIC -I C:\Python35\include -o a.dll a.c` in Windows. – ku' Jan 12 '16 at 06:08
  • How do I get cython to use angle brackets and not quotes? – Gringo Suave Dec 07 '17 at 00:49