【发布时间】:2016-11-24 18:25:45
【问题描述】:
所以我一直在尝试运行一个简单的脚本,我在 MacOS 10.11.5 (El Capitan) 下使用 Cython 0.24 将 C 代码合并到我的 python 项目中。我使用 Python 版本 2.7.10 使用 PyCharm 编辑代码。我的 setup.py 看起来像这样
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import os
dirs = [os.getcwd()]
ext = [Extension("hello_world",
include_dirs=dirs,
sources=["mymodule.c","hello_world.pyx"],
depends=["mymodule.h"])]
setup(
name = "testing",
cmdclass={'build_ext' : build_ext},
ext_modules = cythonize(ext)
)
它生成 hello_world.c 就好了,还生成了一个构建目录和一个 *.so 文件 *.pyx 文件包含以下定义,显然与 mymodule.h 中的定义匹配
cdef extern from "mymodule.h":
cdef float MyFunction(float, float)
我尝试了这里建议的解决方案:"'cc' failed with exit status 1" error when install python library
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
上面代码中已经包含的自定义扩展解决方案来自这里: Compiling Cython with C header files error
我还尝试了此处建议的 MacOS 特定解决方案,但这只是绝望:https://github.com/wesm/feather/issues/61
export MACOSX_DEPLOYMENT_TARGET=10.11
但不幸的是,它仍然无法正常工作。我确信 Cython 工作正常,因为我事先实现了一些功能,并且在 Cython 中实现时可以很好地调用它们。它只包括实际的 C 代码,我遇到了这个问题。
希望有人能帮助我,在此先感谢!
编辑: 我收到以下错误消息
/Users/Me/.pyxbld/temp.macosx-10.11-intel-2.7/pyrex/hello_world.c:263:10: fatal error: 'mymodule.h' file not found
#include "mymodule.h"
^
1 error generated.
Traceback (most recent call last):
File "/Users/Me/Dropbox/Uni/MasterThesis/PythonProjects/cython_hello_world/main.py", line 3, in <module>
import hello_world
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/pyximport/pyximport.py", line 445, in load_module
language_level=self.language_level)
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/pyximport/pyximport.py", line 234, in load_module
exec("raise exc, None, tb", {'exc': exc, 'tb': tb})
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/pyximport/pyximport.py", line 216, in load_module
inplace=build_inplace, language_level=language_level)
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/pyximport/pyximport.py", line 192, in build_module
reload_support=pyxargs.reload_support)
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/pyximport/pyxbuild.py", line 102, in pyx_to_dll
dist.run_commands()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/Cython/Distutils/build_ext.py", line 164, in run
_build_ext.build_ext.run(self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build_ext.py", line 337, in run
self.build_extensions()
File "/Users/Me/Library/Python/2.7/lib/python/site-packages/Cython/Distutils/build_ext.py", line 172, in build_extensions
self.build_extension(ext)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build_ext.py", line 496, in build_extension
depends=ext.depends)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/ccompiler.py", line 574, in compile
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/unixccompiler.py", line 122, in _compile
raise CompileError, msg
ImportError: Building module hello_world failed: ["CompileError: command 'cc' failed with exit status 1\n"]
【问题讨论】:
-
您是否在某处丢失了文件
mymodule.h?它不是由 cython / setup.py 生成的 -
有mymodule.c和mymodule.h,是的。它们都在工作目录中,所以我认为使用 os.getcwd() 作为 include_dir 就足够了
-
我会尝试
ext_modules = ext而不是ext_modules = cythonize(ext)。至少我没见过cythonize()这样叫。 -
是的,这也是一个问题。虽然它并没有解决我的问题。解决方案在我下面的答案中,我想通了。但是感谢您的帮助:)
标签: python c macos pycharm cython