質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

5247閲覧

Cythonのプログラムが正常に動かない

taka8195

総合スコア6

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/09/17 13:24

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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

いろいろと試すうちに問題が解決しました。

どうやら実行手順が間違っていたようで、

python setup.py build_ext --inplace

でhelloworld.pyxをコンパイルしてから、

Python main.py

と入力することで無事にCythonプログラムを実行することに成功しました。

投稿2017/09/23 01:55

taka8195

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問