Pythonの高速化を実現すべくCythonの使用環境を整えようとしていますが、エラーメッセージが出てきてどうにもこうにも先に進めません。どなたかアドバイスをお願いできますか。
試しにmain.pyとhelloworld.pyxのサンプルプログラムを作成し、「python main.py」と入力したところ以下のようなエラーメッセージが表示されてしまいました(文字数制限の都合上、途中省略しています)。
C:\Users\user1\Documents\マイドキュメント\Python\CythonTest>python main.py helloworld.c ライブラリ C:\Users\user1\.pyxbld\temp.win-amd64-3.6\Release\Users\user1\.pyxbld\temp.win-amd64-3.6\Release\pyrex\helloworld.cp36-win_amd64.lib とオブジェクト C:\Users\user1\.pyxbld\temp.win-amd64-3.6\Release\Users\user1\.pyxbld\temp.win-amd64-3.6\Release\pyrex\helloworld.cp36-win_amd64.exp を作成中 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyObject_SetAttr" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyUnicode_InternFromString" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyOS_snprintf" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyThreadState_Get" は未解決で す。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyErr_WarnEx" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyFrame_New" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyErr_Occurred" は未解決です。 ・・・・・ (中略) ・・・・・ helloworld.obj : error LNK2001: 外部シンボル "__imp__PyObject_Call" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp__PyUnicode_FromStringAndSize" は未解決です。 helloworld.obj : error LNK2001: 外部シンボル "__imp___PyObject_GetDictPtr" は未解決です。 C:\Users\user1\.pyxbld\lib.win-amd64-3.6\helloworld.cp36-win_amd64.pyd : fatal error LNK1120: 44 件の未解決の外部参照 Traceback (most recent call last): File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\_msvccompiler.py", line 478, in link self.spawn([self.linker] + ld_args) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\_msvccompiler.py", line 501, in spawn return super().spawn(cmd) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\ccompiler.py", line 909, in spawn spawn(cmd, dry_run=self.dry_run) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\spawn.py", line 38, in spawn _spawn_nt(cmd, search_path, dry_run=dry_run) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\spawn.py", line 81, in _spawn_nt "command %r failed with exit status %d" % (cmd, rc)) distutils.errors.DistutilsExecError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.11.25503\\bin\\HostX86\\x86\\link.exe' failed with exit status 1120 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyximport.py", line 215, in load_module inplace=build_inplace, language_level=language_level) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyximport.py", line 191, in build_module reload_support=pyxargs.reload_support) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyxbuild.py", line 102, in pyx_to_dll dist.run_commands() File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\Cython\Distutils\old_build_ext.py", line 185, in run _build_ext.build_ext.run(self) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\command\build_ext.py", line 339, in run self.build_extensions() File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\Cython\Distutils\old_build_ext.py", line 193, in build_extensions self.build_extension(ext) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\command\build_ext.py", line 558, in build_extension target_lang=language) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\ccompiler.py", line 717, in link_shared_object extra_preargs, extra_postargs, build_temp, target_lang) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\_msvccompiler.py", line 481, in link raise LinkError(msg) distutils.errors.LinkError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.11.25503\\bin\\HostX86\\x86\\link.exe' failed with exit status 1120 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 5, in <module> from helloworld import helloworld File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyximport.py", line 458, in load_module language_level=self.language_level) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyximport.py", line 231, in load_module raise exc.with_traceback(tb) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyximport.py", line 215, in load_module inplace=build_inplace, language_level=language_level) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyximport.py", line 191, in build_module reload_support=pyxargs.reload_support) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\pyximport\pyxbuild.py", line 102, in pyx_to_dll dist.run_commands() File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 955, in run_commands self.run_command(cmd) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py", line 974, in run_command cmd_obj.run() File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\Cython\Distutils\old_build_ext.py", line 185, in run _build_ext.build_ext.run(self) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\command\build_ext.py", line 339, in run self.build_extensions() File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\site-packages\Cython\Distutils\old_build_ext.py", line 193, in build_extensions self.build_extension(ext) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\command\build_ext.py", line 558, in build_extension target_lang=language) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\ccompiler.py", line 717, in link_shared_object extra_preargs, extra_postargs, build_temp, target_lang) File "C:\Users\user1\AppData\Local\Programs\Python\Python36\lib\distutils\_msvccompiler.py", line 481, in link raise LinkError(msg) ImportError: Building module helloworld failed: ["distutils.errors.LinkError: command 'C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2017\\\\Community\\\\VC\\\\Tools\\\\MSVC\\\\14.11.25503\\\\bin\\\\HostX86\\\\x86\\\\link.exe' failed with exit status 1120\n"]
setup.pyを利用する方法も試しましたが、同様にLNK2001のエラーメッセージが量産されて、うまく行きませんでした。
また、「python helloworld.pyx」と入力した場合には、
C:\Users\user1\Documents\マイドキュメント\Python\CythonTest>python helloworld.pyx Hello World
のようにきちんと表示されますが、「python helloworld2.pyx」と入力すると
C:\Users\rando\Documents\マイドキュメント\Python\CythonTest>python helloworld2.pyx File "helloworld2.pyx", line 3 cdef int i ^ SyntaxError: invalid syntax
といったエラーメッセージが表示されることがわかっています。ちなみに、「cython helloworld2.pyx」と入力した場合はエラーが出ませんでした(この場合「Hello World」は表示されません)。
どのようにすれば、Cythonを正常に動かすことができますでしょうか。
現在の環境は以下の通りです。
OS:Win10 64bit
Python: 3.6.2
Cython: 0.26.1
VS2017: 15.3.4
なお、ソースコードを書くときはAtomを使用しており、VS2017はC言語のコンパイラをインストールするためにPCに入れました。
###main.py
Python
1import pyximport; pyximport.install() 2 3import helloworld
###helloworld.pyx
Python
1print("Hello World")
###setup.py
Python
1from distutils.core import setup 2from distutils.extension import Extension 3from Cython.Distutils import build_ext 4 5setup( 6 cmdclass = {'build_ext': build_ext}, 7 ext_modules = [Extension("helloworld", ["helloworld.pyx"])] 8)
###helloworld2.pyx
Python
1print("Hello World") 2 3cdef int i 4cdef double sum=0.0 5 6for i in range(5000): 7 sum += i 8 9print(sum)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。