因此,我一直在尝试运行一个简单的脚本,在该脚本中,我在MacOS 10.11.5(El Capitan)下使用Cython 0.24将C代码合并到我的python项目中。我使用PyCharm和Python版本2.7.10编辑代码。我的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)
我尝试了此处建议的解决方案:安装python库时出现“'cc'失败,退出状态为1”的错误
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
并且上面代码中已经存在的自定义扩展解决方案来自这里:使用C头文件编译Cython错误
我还尝试了此处建议的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"]
我自己解决了。问题实际上不在我在此处发布的代码中,而是在调用该函数的代码中。我有台词
import pyximport
pyximport.install()
仍然在代码中,然后在hello_world中调用该函数。这似乎引起了问题。当我删除这两行时,可以很好地调用该函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句