2016-05-25 3 views
0

私はcythonを初めて使用しています。私は1つのソースコードファイルhello.pyxありますCythonチュートリアルのcpdefが機能しない

cdef extern from "math.h": 
    cpdef double sin(double x) 

をして、私のsetup.pyファイルは次のとおりです。その後、私は.soにそれをコンパイル

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

ext_modules=[ 
    Extension("hello", 
       sources=["hello.pyx"], 
       libraries=["m"] # Unix-like specific 
    ) 
] 

setup(
name = "Demos", 
ext_modules = cythonize(ext_modules) 
) 

しかし、私がimport helloになったとき、私はhello.sinの機能を持っていません。

"これはPythonコードのC sin()関数への直接アクセスを提供するCythonモジュールです:"チュートリアルで書かれていますか?

外部宣言でofficial tutorialに従っています。 cythoningを実行するための

結果:

Compiling hello.pyx because it changed. 
Cythonizing hello.pyx 
running build_ext 
building 'hello' extension 
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall-Wstrict-prototypes -fPIC -I/pool/software/python/python27plus-ibm/include/python2.7 -c hello.c -o build/temp.linux-x86_64-2.7/hello.o 
gcc -pthread -shared build/temp.linux-x86_64-2.7/hello.o -L/proj/dist/sandbox/miniconda/lib -lm -lpython2.7 -o /home/shaowu/Documents/cython_play/hello.so 
+0

モジュールはインポートされますが、そこには「sin」関数はありません。 'hello.sin'にアクセスしようとすると、あなたは' AttributeError'を取得しますか? –

+0

@Jim Correct。私が 'dir(hello)'をするとき、 'sin 'はありません。理由はわかりません。 – Shaowu

+1

私は使用しているバージョンをチェックします。私の印象は、これは数年前に追加されたので、おそらくあなたは古いです。 – DavidW

答えて

1

を@DavidWが述べたように、これはCythonの古いバージョンを使用して起因するあなたに最も可能性が高いです。

from distutils.core import setup 
from distutils.extension import Extension 
from Cython.Distutils import build_ext 

ext_modules=[ 
    Extension("demo", 
       ["demo.pyx"], 
       libraries=["m"]) # Unix-like specific 
] 

setup(
    name = "Demos", 
    cmdclass = {"build_ext": build_ext}, 
    ext_modules = ext_modules 
) 

いずれに理想的に、それを使用するか、install the latest version:バージョン< 0.22あなたsetup.pyスクリプト、ドキュメントによると、should look like thisで。

0

私はここに、私のラップトップ上の問題はありませんが、私がやった手順は以下のとおりです。hello.pyxsetup.pytest.py

  • を含むディレクトリへ

    • cdpython setup.py build_ext --inplace
    • python test.py

    これが返されます。

    ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__test__', 'sin'] 
    0.479425538604203 
    

    test.pyで含む:

    import hello 
    
    print(dir(hello)) 
    print(hello.sin(0.5)) 
    
  • 関連する問題