前提・実現したいこと
インターネットにて「Pythonを簡単高速化」という記事を見て、
「cython」を利用することで実現できるということで、
試験的にその記事のコードをそのまま、入力して実行したところ、
以下のエラーメッセージが表示されました。
"Error compiling Cython file"と表示されていますので、
Cythonのバージョンが合っていない等の類なのかと思っておりますが、
具体的にどう対処すれば分かりかねておりますので、
ご教示いただければ幸いです。
発生している問題・エラーメッセージ
Error compiling Cython file: ------------------------------------------------------------ ... a, b = 0, 1 for i in range(n): a, b = a + b, a return a %timeit fib_cython(5000) ^ ------------------------------------------------------------ .ipython\cython\_cython_magic_c7814a59100b6d13ed17eacd0e131b61.pyx:8:0: Expected an identifier or literal --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-4ed1b8fcc8ec> in <module>() ----> 1 get_ipython().run_cell_magic('cython', '', '\ndef fib_cython(n):\n a, b = 0, 1\n for i in range(n):\n a, b = a + b, a\n return a\n\n%timeit fib_cython(5000)') ~\Anaconda3\envs\Py3.5\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2165 magic_arg_s = self.var_expand(line, stack_depth) 2166 with self.builtin_trap: -> 2167 result = fn(magic_arg_s, cell) 2168 return result 2169 <C:\Users\HINOKI_HARA\Anaconda3\envs\Py3.5\lib\site-packages\decorator.py:decorator-gen-128> in cython(self, line, cell) ~\Anaconda3\envs\Py3.5\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k) 185 # but it's overkill for just that one bit of state. 186 def magic_deco(arg): --> 187 call = lambda f, *a, **k: f(*a, **k) 188 189 if callable(arg): ~\Anaconda3\envs\Py3.5\lib\site-packages\Cython\Build\IpythonMagic.py in cython(self, line, cell) 319 if need_cythonize: 320 extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet) --> 321 assert len(extensions) == 1 322 extension = extensions[0] 323 self._code_cache[key] = module_name TypeError: object of type 'NoneType' has no len()
該当のソースコード
python
1In [1]: %load_ext Cython 2 3In [2]: %%cython 4 5 def fib_cython(n): 6 a, b = 0, 1 7 for i in range(n): 8 a, b = a + b, a 9 return a 10 11%timeit fib_cython(5000) 12
試したこと
実行速度を計測するマジックコマンド行の
「%timeit fib_cython(5000)」を
別セルに入力して実行してみましたが、この場合
「NameError: name 'fib_cython' is not defined」
と別のエラーが表示されてしまい、解決に至っておりません。
補足情報(FW/ツールのバージョンなど)
環境は、
Windows8.1 Pro 64bit
Anaconda3(64bit版)の仮想環境下で、
Python 3.5.6
jupyternotebook 5.6.0
ipython 6.5.0
cython 0.28.5がインストールされています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/31 01:44
2019/09/02 06:28 編集