Python.h未发现使用痛饮和Python的蟒蛇 [英] Python.h not found using swig and Anaconda Python
问题描述
我试图编译下面这个教程一个简单的Python / C例如:
I'm trying to compile a simple python/C example following this tutorial:
http://www.swig.org/tutorial.html
我在使用蟒蛇蟒蛇MacOS的。
I'm on MacOS using Anaconda python.
,当我运行
gcc -c example.c example_wrap.c -I/Users/myuser/anaconda/include/
我得到:
example_wrap.c:130:11: fatal error: 'Python.h' file not found
# include <Python.h>
^
看来,这个问题是在一些问题报道:
It seems that this problem is reported in a number of questions:
<一个href=\"http://stackoverflow.com/questions/4097339/missing-python-h-while-trying-to-compile-a-c-extension-module\">Missing Python.h试图编译C扩展模块
但似乎没有提供在MacOS特定蟒蛇答案
but none seem to provide an answer specific to Anaconda on MacOS
任何人都解决了这个?
推荐答案
使用该选项 -I /用户/ myuser的/蟒蛇/有/ python2.7
在 GCC
命令。 (这是假设你正在使用python 2.7,将名称更改为匹配您正在使用python的版本。)您可以使用命令中的python-配置--cflags
来获得全套推荐编译标志的:
Use the option -I/Users/myuser/anaconda/include/python2.7
in the gcc
command. (That's assuming you are using python 2.7. Change the name to match the version of python that you are using.) You can use the command 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
不过,要构建扩展模块,我建议使用一个简单的安装脚本,如下面的 setup.py
,并让的distutils
找出所有的编译和链接选项给你。
However, to build the extension module, I recommend using a simple setup script, such as the following setup.py
, and let distutils
figure out all the compiling and linking 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
(看看那些回显到终端编译命令时, setup.py
运行。)
的distutils
知道痛饮,所以不是包括 example_wrap.c
在源文件列表中,你可以包括 example.i
和痛饮
将被安装脚本自动运行:
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 be run automatically by the setup 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"])
随着 setup.py
上面的版本,可以用单一的命令来构建扩展模块
With the above version of setup.py
, you can build the extension module with the single command
$ python setup.py build_ext --inplace
一旦你建立了扩展模块,你应该能够在Python中使用它:
Once you've built the extension module, you should be able to use it in python:
>>> import example
>>> example.fact(5)
120
如果你不想使用该脚本 setup.py
,这里有一组为我工作的命令:
If you'd rather not use the script setup.py
, here's 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
请注意:我使用的是Mac OS X 10.9.4:
Note: I'm 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
这篇关于Python.h未发现使用痛饮和Python的蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!