Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python.h header file missing on Mac OS X 10.6

I'm trying to access a shared C library in Python with ctypes on Mac OS X 10.6.8 with Python 2.7.4. To do this, I need to #include <Python.h> in my C code. If I try to compile a C script that only has that one include statement in it, call it "sample.c", I get:

$ gcc -shared -o sample.so sample.c
sample.c:1:20: error: Python.h: No such file or directory

Since I'm running Mac 10.6, I have Xcode 3.2.6, the latest version available on this iteration of OS X without paying to upgrade to 10.7 and getting Xcode 4. Is there a way to get the Python header file without upgrading my OS?

like image 604
Brett Morris Avatar asked May 09 '13 04:05

Brett Morris


People also ask

Where are Python header files?

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.

Where are header files stored in Mac?

This form is used when the header file is located in the directory /usr/include/directory, where directory is a subdirectory of /usr/include. #include "headername. h"

Where are opengl header files Mac?

In MacOS 10.12. 6, they're under /opt/X11/include , e.g. /opt/X11/include/GL/gl. h .

Are there header files in Python?

No, Python does not have header files nor similar. Neither does Java, despite your implication that it does. Instead, we use "docstrings" in Python to make it easier to find and use our interfaces (with the built-in help() function).


4 Answers

Python is a framework on Mac OS X so you need to,

#include <Python/Python.h>

You also need to call gcc with the -framework argument to actually do anything inside C,

gcc -shared -o sample.so sample.c -framework Python
like image 83
Jared Avatar answered Nov 11 '22 12:11

Jared


I'm not sure about 10.6.8, but Python.h should be in

/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

if you installed the official python.org binary. Try adding

-I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7

to your gcc command and see if that works.

like image 24
MattDMo Avatar answered Nov 11 '22 11:11

MattDMo


In case you have installed Python using Brew, it may be worthwhile to check the location of where your headers are. Try I/usr/local/Cellar/python/...

like image 31
Surya Shekhar Chakraborty Avatar answered Nov 11 '22 12:11

Surya Shekhar Chakraborty


Another way is to add `python-config --include` to the gcc call. It will expand to -I/usr/..., so

gcc -shared -o sample.so sample.c `python-config --include`

Also other options can be retrieved, such as `python-config --cflags --ldflags`.

like image 33
ttq Avatar answered Nov 11 '22 13:11

ttq