简体   繁体   English

成功编译后无法导入已编译的cython函数

[英]Can't import compiled cython function after successful compilation

I'm testing out Cython on Windows 10 (Spyder 3.2.3) and Python 3.6.3 following the Tutorial , and I wrote this function: 我在Windows 10(Spyder的3.2.3)和Python 3.6.3测试出用Cython以下教程 ,和我写了这个功能:

from libc.stdlib cimport malloc, free

cdef int** t3(int* El, int lEl):
    cdef int lElsq = lEl*lEl
    cdef int i,j
    cdef int** rr = <int**> malloc(2*sizeof(int*))
    for i in range(2):
        rr[i] = <int*> malloc(lElsq*sizeof(int))
    for i in range(lEl):
        for j in range(lEl):
            rr[0][i*lEl+j] = El[i]
            rr[1][i*lEl+j] = El[j]
    return rr

I put it into a pyx file and following the tutorial compiled it using the MS Visual Studio 14 compiler. 我将其放入pyx文件中,并按照教程中的说明使用MS Visual Studio 14编译器对其进行了编译。

Command: %run setup.py build_ext --inplace 命令: %run setup.py build_ext --inplace

Setup file: 设置文件:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("tempCython.pyx"),
)

The compilation didn't throw any error and a lot of files were created: 编译没有引发任何错误,并且创建了许多文件:

In the same directory as the original file: 与原始文件位于同一目录中:

  • a tempCython.c file of the same name tempCython.c文件
  • a tempCython.cp36-win_amd64.pyd file tempCython.cp36-win_amd64.pyd文件

In the subdirectory build\\temp.win-amd64-3.6\\Release : 在子目录build\\temp.win-amd64-3.6\\Release

  • a tempCython.obj file tempCython.obj文件
  • a tempCython.cp36-win_amd64.exp file tempCython.cp36-win_amd64.exp文件
  • a tempCython.cp36-win_amd64.lib file tempCython.cp36-win_amd64.lib文件

The Cython tutorial tells me I need the pyd file, so I removed the cp36-win_amd64 in the filename. Cython教程告诉我我需要pyd文件,因此我删除了文件名中的cp36-win_amd64

I can import it without error, but my function t3 just never shows up, it's simply not there. 我可以毫无错误地导入它,但是我的函数t3永远不会出现,根本就不存在。

Without any kind of error message I'm at a loss now. 没有任何错误消息,我现在很茫然。 I have encountered errors like these before sometimes in 3rd-party packages, but I never understood what caused them. 有时我在第三方包装中遇到过此类错误,但我从来不明白是什么原因造成的。 Why is my function not there? 为什么我的功能不存在?

I'd also like to know the point of all those additional files, which ones do I actually need? 我还想知道所有这些其他文件的重点,我实际上需要哪些文件?

You have written a cdef function and are trying to call it from python. 您已经编写了一个cdef函数,并试图从python调用它。 Unfortunately, cdef functions are not visible from python (see http://docs.cython.org/en/latest/src/userguide/language_basics.html#python-functions-vs-c-functions ) so you need to do a little bit more work to expose your function to python. 不幸的是, cdef函数在python中不可见(请参阅http://docs.cython.org/en/latest/src/userguide/language_basics.html#python-functions-vs-c-functions ),因此您需要做一些事情将您的函数暴露给python的更多工作。 In particular you would need to write either a def function or a cpdef function that calls your cdef function and then call that function from python. 特别是,您需要编写def函数或cpdef函数来调用cdef函数,然后从python调用函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM