使用 swig 和 Anaconda Python 找不到 Python.h

时间:2023-01-24
本文介绍了使用 swig 和 Anaconda Python 找不到 Python.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试按照本教程编译一个简单的 python/C 示例:

I'm trying to compile a simple python/C example following this tutorial:

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

我在 MacOS 上使用 Anaconda python.

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:

在尝试编译时缺少 Python.hC扩展模块

缺少 Python.h 且无法找到

Python.h:没有这样的文件或目录

但似乎没有一个提供特定于 MacOS 上 Anaconda 的答案

but none seem to provide an answer specific to Anaconda on MacOS

有人解决了吗?

推荐答案

gcc-I/Users/myuser/anaconda/include/python2.7选项> 命令.(假设您使用的是 python 2.7.更改名称以匹配您正在使用的 python 版本.)您可以使用命令 python-config --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"])

然后就可以运行了:

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

(查看运行 setup.py 时回显到终端的编译器命令.)

(Take a look at the compiler commands that are echoed to the terminal when setup.py is run.)

distutils 了解 SWIG,因此您可以在源文件列表中包含 example_wrap.c,而不是包含 example.iswig 将由安装脚本自动运行:

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

这篇关于使用 swig 和 Anaconda Python 找不到 Python.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在 Anaconda 上升级到 scikit-learn 的开发版? 下一篇:在 anaconda spyder 中导入自己的 .py 文件

相关文章

最新文章