Cython'module'对象没有属性

时间:2016-01-08 19:12:08

标签: python cython

在我的pyx文件中,我定义了两个C函数并由python函数包装。 它已成功编译并输出名为Rand_Motion.cpython-34m.so的文件。 但是,我无法使用导入Rand_Motion访问主文件中pyx文件中定义的任一函数。这是我的Rand_Motion.pyx

cdef extern from "math.h":
    float cosf(float theta)  
    float sinf(float theta)          
    float acosf(float theta)    
    float sqrt(float x)

cdef object randmotion_c(int Dimension,float Diffcoe, float pos_x,float pos_y, float pos_z,float drift_l,float drift_a,float drift_b,float a,float b):
    cdef float length=sqrt(2*Dimension*Diffcoe)
    cdef float pi= 3.1415926535
    cdef float e=  2.7182818284
    cdef float a_d=a*2*pi
    cdef float b_d=acosf(1-b*2)

    pos_x+=length*sinf(b_d) *cosf(a_d)
    pos_y+=length*sinf(b_d) *sinf(a_d)
    pos_z+=length*cosf(b_d)    

    if  drift_l>0:
        drift_a=(drift_a/180)*pi
        drift_b=(drift_b/180)*pi
        pos_x+=drift_l*sinf(drift_a) *cosf(drift_b)
        pos_y+=drift_l*sinf(drift_a) *sinf(drift_b) 
        pos_z+=drift_l*cosf(drift_a)        
    return pos_x,pos_y,pos_z

def randmotion(Dimension,Diffcoe, pos_x,pos_y,pos_z,drift_l,drift_a,drift_b,a,b):

    return randmotion_c(Dimension,Diffcoe, pos_x,pos_y,pos_z,drift_l,drift_a,drift_b,a,b)

cdef dis_cal_c(float x,float y,float z):
    return sqrt(x*x+y*y+z*z)

def dis_cal(x,y,z):
    return dis_cal_c(x,y,z) 

1 个答案:

答案 0 :(得分:1)

我将您的代码复制/粘贴到aaa.pyx文件中,然后我运行了:

mgc@mgc-X:~/code/test$ cythonize aaa.pyx
mgc@mgc-X:~/code/test$ gcc -shared -fPIC -fwrapv -O2 -Wall -I/usr/include/python3.4 -o aaa.so aaa.c
mgc@mgc-X:~/code/test$ python3
Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from aaa import randmotion, dis_cal
>>> dis_cal(1,2,3)
3.7416573867739413

它似乎工作得很好(请注意,我在与共享对象相同的目录中运行python )。 也许你想安装你的共享库?请查看cython documentation about compilationpython documentation about installing modules