1

操作系统 X,10.7。我已经浏览了 cython 教程中的前几个 cython 示例,到目前为止,所有教程示例对我来说都是正确的。

但是,当我尝试执行 Fernando 在加拿大 Pycon 的 Science And Python 演示中所做的事情时(播放位置 37:42),我收到了一个奇怪的错误。代码是:

%load_ext cythonmagic
%%cython
cdef double fcy(double x) except? -2:
    return x**2-x

def integrate_fcy(double a, double b, int N):
    cdef int i
    cdef double s, dx
    s = 0; dx = (b-a)/N
    for i in range(N):
    s += fcy(a+i*dx)
    return s * dx

笔记本输出:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-72-e327d909d703> in <module>()
----> 1 get_ipython()._run_cached_cell_magic('cython', '')

/usr/local/lib/python3.2/site-packages/IPython/core/interactiveshell.py in _run_cached_cell_magic(self, magic_name, line)
   2513         cell = self._current_cell_magic_body
   2514         self._current_cell_magic_body = None
-> 2515         return self.run_cell_magic(magic_name, line, cell)
   2516 
   2517     def run_cell(self, raw_cell, store_history=False, silent=False):

/usr/local/lib/python3.2/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2092             magic_arg_s = self.var_expand(line, stack_depth)
   2093             with self.builtin_trap:
-> 2094                 result = fn(line, cell)
   2095             return result
   2096 

/usr/local/lib/python3.2/site-packages/IPython/extensions/cythonmagic.py in cython(self, line, cell)

/usr/local/lib/python3.2/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    190     # but it's overkill for just that one bit of state.
    191     def magic_deco(arg):
--> 192         call = lambda f, *a, **k: f(*a, **k)
    193 
    194         if isinstance(arg, collections.Callable):

/usr/local/lib/python3.2/site-packages/IPython/extensions/cythonmagic.py in cython(self, line, cell)
    187             self._code_cache[key] = module_name
    188 
--> 189         module = imp.load_dynamic(module_name, module_path)
    190         self._import_all(module)
    191 

ImportError: dlopen(/Users/lab/.ipython/cython/_cython_magic_0e71fc3338606be06aed17a605e60bbd.cpython-32m.so, 2): image not found

这是我用来启动 IPython 笔记本会话的终端中的输出:

/Users/lab/.ipython/cython/_cython_magic_e9a2648cd85a2b3bce8e97b6bbc5f379.c:467:15: warning: 
      unused function
      '__pyx_f_46_cython_magic_e9a2648cd85a2b3bce8e97b6bbc5f379_fcy'
      [-Wunused-function]
static double __pyx_f_46_cython_magic_e9a2648cd85a2b3bce8e97b6bbc5f379_fcy(...
              ^
1 warning generated.

这对我来说没问题,但不是上面的 %%cython 魔法:

a=10
b=20

%%cython_inline
return a+b

Compiling /Users/lab/Library/Caches/cython/inline/_cython_inline_074486d5a226d2c163f10589ac495ca2.pyx because it changed.
Cythonizing /Users/lab/Library/Caches/cython/inline/_cython_inline_074486d5a226d2c163f10589ac495ca2.pyx
Out[52]: 30

有人知道这里有什么问题吗?

4

0 回答 0