最终更新
这个问题是关于如何写一个setup.py
这将编译一个 cython 模块,该模块可以像 C 一样直接访问 FORTRAN 代码。这是一个相当漫长而艰巨的解决方案旅程,但完整的混乱情况包含在下面作为上下文。
原问题
我有一个扩展,它是一个 Cython 文件,它设置一些堆内存并将其传递给 fortran 代码,还有一个 fortran 文件,它是一个古老的旧模块,如果可以的话,我希望避免重新实现。
The .pyx
文件可以很好地编译为 C,但 cython 编译器会卡住.f90
文件出现以下错误:
$ python setup.py build_ext --inplace
running build_ext
cythoning delaunay/__init__.pyx to delaunay/__init__.c
building 'delaunay' extension
error: unknown file type '.f90' (from 'delaunay/stripack.f90')
这是我的设置文件(上半部分):
from distutils.core import setup, Extension
from Cython.Distutils import build_ext
ext_modules = [
Extension("delaunay",
sources=["delaunay/__init__.pyx",
"delaunay/stripack.f90"])
]
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules,
...
)
注意:我最初错误地指定了 fortran 文件的位置(没有目录前缀),但在修复该问题后,它以完全相同的方式中断。
我尝试过的事情:
I found this,并尝试传递 fortran 编译器的名称(即 gfortran),如下所示:
$ python setup.py config --fcompiler=gfortran build_ext --inplace
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: option --fcompiler not recognized
我也尝试过删除--inplace
,以防这就是问题所在(事实并非如此,与顶部错误消息相同)。
那么,我该如何编译这个fortran呢?我可以把它破解成.o
我自己并摆脱链接它?或者这是 Cython 中的错误吗,这将迫使我重新实现 distutils 或使用预处理器进行修改?
UPDATE
所以,检查了numpy.distutils
包,我更了解这个问题了。看来你必须
- 使用 cython 将 .pyx 文件转换为 cpython .c 文件,
- 然后使用一个
Extension
/setup()
支持 fortran 的组合,例如numpy
's.
尝试过这个之后,我的setup.py
现在看起来像这样:
from numpy.distutils.core import setup
from Cython.Build import cythonize
from numpy.distutils.extension import Extension
cy_modules = cythonize('delaunay/sphere.pyx')
e = cy_modules[0]
ext_modules = [
Extension("delaunay.sphere",
sources=e.sources + ['delaunay/stripack.f90'])
]
setup(
ext_modules = ext_modules,
name="delaunay",
...
)
(请注意,我还对模块进行了一些重组,因为看起来似乎是__init__.pyx
是不允许的...)
现在事情变得有问题并且依赖于平台。我有两个可用的测试系统 - 一个是 Mac OS X 10.6 (Snow Leopard),使用 Macports Python 2.7,另一个是 Mac OS X 10.7 (Lion),使用系统 python 2.7。
在 Snow Leopard 上,以下内容适用:
这意味着该模块可以编译(万岁!)(尽管没有--inplace
对于 numpy,看来,所以我必须在系统范围内安装测试模块:/),但我仍然遇到崩溃import
如下:
>>> import delaunay
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<snip>site-packages/delaunay/__init__.py", line 1, in <module>
from sphere import delaunay_mesh
ImportError: dlopen(<snip>site-packages/delaunay/sphere.so, 2): no suitable image found. Did find:
<snip>site-packages/delaunay/sphere.so: mach-o, but wrong architecture
在 Lion 上,我收到一个编译错误,后面是一条看起来相当混乱的编译行:
gfortran:f77: build/src.macosx-10.7-intel-2.7/delaunay/sphere-f2pywrappers.f
/usr/local/bin/gfortran -Wall -arch i686 -arch x86_64 -Wall -undefined dynamic_lookup -bundle build/temp.macosx-10.7-intel-2.7/delaunay/sphere.o build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/delaunay/spheremodule.o build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/fortranobject.o build/temp.macosx-10.7-intel-2.7/delaunay/stripack.o build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/delaunay/sphere-f2pywrappers.o -lgfortran -o build/lib.macosx-10.7-intel-2.7/delaunay/sphere.so
ld: duplicate symbol _initsphere in build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/delaunay/spheremodule.o ldand :build /temp.macosx-10.7-intelduplicate- 2.7symbol/ delaunay/sphere.o _initsphere in forbuild architecture /i386
temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/delaunay/spheremodule.o and build/temp.macosx-10.7-intel-2.7/delaunay/sphere.o for architecture x86_64
现在让我们先退后一步,然后再仔细研究这里的细节。首先,我知道 64 位 Mac OS X 中的架构冲突存在很多令人头疼的问题;我必须非常努力地让 Macports Python 在 Snow Leopard 机器上运行(只是为了从系统 python 2.6 升级)。我也知道当你看到gfortran -arch i686 -arch x86_64
您正在向编译器发送混合消息。其中隐藏着各种特定于平台的问题,我们无需在这个问题的背景下担心。
但让我们看看这一行:
gfortran:f77: build/src.macosx-10.7-intel-2.7/delaunay/sphere-f2pywrappers.f
numpy 在做什么?我在此版本中不需要任何 f2py 功能!我实际上写了一个 cython 模块为了避免处理 f2py 的疯狂(我需要有 4 或 5 个输出变量,以及既不进也不出的参数 - f2py 中都没有很好地支持这两个参数。)我只想编译它.c
-> .o
, and .f90
-> .o
并将它们链接起来。如果我知道如何包含所有相关的标头,我可以自己编写此编译器行。
请告诉我,我不需要为此编写自己的 makefile...或者有一种方法可以将 fortran 转换为(输出兼容的)C,这样我就可以避免 python 看到 .f90 扩展名(它修复了整个问题)问题。)请注意f2c不适合这个,因为它只适用于 F77,这是一种更现代的方言(因此.f90
文件扩展名)。
UPDATE 2以下 bash 脚本将愉快地编译并链接代码:
PYTHON_H_LOCATION="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/"
cython sphere.pyx
gcc -arch x86_64 -c sphere.c -I$PYTHON_H_LOCATION
gfortran -arch x86_64 -c stripack.f90
gfortran -arch x86_64 -bundle -undefined dynamic_lookup -L/opt/local/lib *.o -o sphere.so
关于如何使这种 hack 与 setup.py 兼容有什么建议吗?我没有任何人安装这个模块必须去寻找Python.h
手动...