Python.h not found using swig and Anaconda Python

I am trying to compile a simple python / C example following this guide:

http://www.swig.org/tutorial.html

I am on MacOS using python Anaconda.

however when i run

gcc -c example.c example_wrap.c -I/Users/myuser/anaconda/include/ 

I get:

 example_wrap.c:130:11: fatal error: 'Python.h' file not found # include <Python.h> ^ 

This issue seems to be reported on a number of issues:

Python.h is missing when trying to compile a C extension module

Python.h is missing and impossible to find

Python.h: No such file or directory

but no one seems to give an answer specific to Anaconda on macOS

Has anyone solved this?

+6
source share
1 answer

Use the -I/Users/myuser/anaconda/include/python2.7 command in the gcc command. (Suppose you are using python 2.7. Change the name to match the version of python you are using.) You can use the python-config --cflags to get the full set of recommended compilation flags:

 $ python-config --cflags -I/Users/myuser/anaconda/include/python2.7 -I/Users/myuser/anaconda/include/python2.7 -fno-strict-aliasing -I/Users/myuser/anaconda/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes 

However, to build an extension module, I recommend using a simple setup script, for example, the following setup.py , and let distutils display all the compilation and layout options for you.

 # setup.py from distutils.core import setup, Extension example_module = Extension('_example', sources=['example_wrap.c', 'example.c']) setup(name='example', ext_modules=[example_module], py_modules=["example"]) 

Then you can run:

 $ swig -python example.i $ python setup.py build_ext --inplace 

(Look at the compiler commands that echo to the terminal when running setup.py .)

distutils knows about SWIG, so instead of including example_wrap.c in the list of source files, you can include example.i , and swig will start automatically by setting the script:

 # setup.py from distutils.core import setup, Extension example_module = Extension('_example', sources=['example.c', 'example.i']) setup(name='example', ext_modules=[example_module], py_modules=["example"]) 

In the setup.py version above, you can create an extension module with a single command

 $ python setup.py build_ext --inplace 

After creating the extension module, you can use it in python:

 >>> import example >>> example.fact(5) 120 

If you prefer not to use setup.py script, here is a set of commands that worked for me:

 $ swig -python example.i $ gcc -c -I/Users/myuser/anaconda/include/python2.7 example.c example_wrap.c $ gcc -bundle -undefined dynamic_lookup -L/Users/myuser/anaconda/lib example.o example_wrap.o -o _example.so 

Note. I am using Mac OS X 10.9.4:

 $ gcc --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.3.0 Thread model: posix 
+13
source

Source: https://habr.com/ru/post/975385/


All Articles