1

so I've been trying to get a simple script running, where I incorporate C-code into my python project using Cython 0.24 under MacOS 10.11.5 (El Capitan). I edit the code with PyCharm with Python Version 2.7.10. My setup.py looks like this

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)
)

It generates the hello_world.c just fine and also generates a build directory and an *.so file The *.pyx file contains the following definition, which obviously matches the definition in mymodule.h

cdef extern from "mymodule.h":
    cdef float MyFunction(float, float)

I tried the solution suggested here: "'cc' failed with exit status 1" error when install python library

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

and the custom extension solution already in above code came from here: Compiling Cython with C header files error

I also tried a MacOS specific solution suggested here, but that was just desperation: https://github.com/wesm/feather/issues/61

export MACOSX_DEPLOYMENT_TARGET=10.11

But unfortunately it still won't work. I am sure that Cython is working properly because I implemented some functions beforehand and could call them just fine, when the implementation was in Cython. It's just including actual C code, where this problem occurs for me.

I hope someone can help me, thanks in advance!

edit: I get the following error messages

/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"]
Community
  • 1
  • 1
meetaig
  • 913
  • 10
  • 26
  • Do you that missing file `mymodule.h` somewhere? It is not generated by cython / setup.py – J.J. Hakala Jul 21 '16 at 12:47
  • there is mymodule.c and mymodule.h, yes. They are all in the working directory so I thought it would be sufficient to use os.getcwd() as include_dir – meetaig Jul 21 '16 at 12:57
  • I would try `ext_modules = ext` instead of `ext_modules = cythonize(ext)`. At least I haven't seen `cythonize()` called like that. – J.J. Hakala Jul 21 '16 at 13:02
  • Yes that was a problem as well. It did not fix my issue though. The solution to that is in my answer below, I figured it out. But thanks for helping :) – meetaig Jul 21 '16 at 13:39

1 Answers1

0

I solved it on my own. The problem was actually not in the code I posted here but in the code calling the function. I had the lines

import pyximport
pyximport.install()

still in the code before calling the function in hello_world. This seems to have caused the problem. When I removed those two lines, the function could be called just fine.

meetaig
  • 913
  • 10
  • 26