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

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

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

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

Python

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

Q&A

解決済

1回答

2643閲覧

Python.hが使えない.

aaaa____

総合スコア26

Python 3.x

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

Python

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

0グッド

0クリップ

投稿2022/10/07 13:02

編集2022/10/09 12:14

やろうとしてること

https://qiita.com/kawamou/items/4235e37441e22205de34
このサイトの,「GoからPythonをよぶ場合」というのを実行しようとしています.

環境

macOS Big Sur ver. 11.5.1(20G80)  -> 12.6 (21G115)

MacBook Air M1, 2020

メモリ 8GB

Homebreq ver.3.6.3(git revision 86ac48865b7; last commit 2022-10-02)

Python 3.10.6

pip 22.2.2 from /opt/homebrew/lib/python3.10/site-packages/pip (python 3.10)

pyaudio-0.2.12

問題

完成コードが示されているのですが,確認してみたところ,自分のとこのコードではディレクトリ構成がことなっているので ,cgoの部分を変更する必要がありました.
CFLAGSのpythonの場所は,

/Library/Developer/CommandLineTools/usr/bin

にあり,LDFLAGSのlibは

/Users/usrname/Library/Python/3.9/lib

にあったのでコードを
main.go

Go

1package main 2 3// #cgo CFLAGS: -I/Library/Developer/CommandLineTools/usr/bin/python3.9 4// #cgo LDFLAGS: -L/Users/usrname/Library/Python/3.9/lib python 5// #include <Python.h> 6import "C" 7 8func main() { 9 // 最後にPythonインタプリタ終了 10 defer C.Py_Finalize() 11 // Pythonインタプリタの初期化 12 C.Py_Initialize() 13 // GoのstringをCのcharに型変換(変換しないとPyRun_SimpleStringに型合ってないよって怒られる) 14 // cannot use "print(\"Hello, World!\")" (type string) as type *_Ctype_char in argument to _Cfunc_PyRun_SimpleString 15 pyCodeStr := `print("Hello, World!")` 16 pyCodeChar := C.CString(pyCodeStr) 17 // Pythonコードを文字列として受け取ってインタプリタ上で実行 18 C.PyRun_SimpleString(pyCodeChar) 19}

のようにしました.
この状態でビルドを行うと

MacBook-Air sound_wav % go build -o main go build sound_wav: invalid flag in #cgo LDFLAGS: python

と言われたので,調べて
https://stackoverflow.com/questions/62580446/invalid-flag-in-cgo-ldflags
このサイトに辿り着き,元の完成コードにもあった"-l"をLDFLAGSの方につけなくてはいけないのかもしれないとおもい.

// #cgo CFLAGS: -I/Library/Developer/CommandLineTools/usr/bin/python3.9 // #cgo LDFLAGS: -L/Users/usrname/Library/Python/3.9/lib -lpython // #include <Python.h>

と修正し,再度ビルドすると

MacBook-Air sound_wav % go build -o main # sound_wav ./main.go:5:11: fatal error: 'Python.h' file not found #include <Python.h> ^~~~~~~~~~ 1 error generated.

と出力されました.
これもまた調べると,
https://mio.yokohama/?p=1235
このサイトが出てきたので,書いてあるように,verが3.10.6だったので,それに対応するのをPythonのサイトからGzipped source tarballで持ってきて,そこにあるIncludemをLDFLAGSのPATHにあるpythonの下に入れました.

しかし,その後ビルドしても,Python.hが見つからないと言われ,それでは強硬策をとおもい,main.goと同じディレクトリにIncludeディレクトリをコピーして.

// #include <Include/Python.h>

としたのですが,

MacBook-Air sound_wav % go build -o main # sound_wav In file included from ./main.go:5: ./Include/Python.h:8:10: fatal error: 'pyconfig.h' file not found #include "pyconfig.h" ^~~~~~~~~~~~ 1 error generated.

と,pyconfig.hがないと言われました.
実際にincludeディレクトリにはこのファイルはなかったので.
https://svn.python.org/projects/python/trunk/PC/pyconfig.h
このサイトのをコピペしてincludeディレクトリにおくと,

#include <io.h>

が使えないと出ていたので,コメントアウト(拡張機能によるエラーの通知はなかった)し,再度ビルドしたのですが.

MacBook-Air sound_wav % go build -o main # sound_wav In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:73:9: warning: 'PY_UINT32_T' macro redefined [-Wmacro-redefined] #define PY_UINT32_T uint32_t ^ ./Include/pyconfig.h:402:9: note: previous definition is here #define PY_UINT32_T unsigned int ^ In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:74:9: warning: 'PY_UINT64_T' macro redefined [-Wmacro-redefined] #define PY_UINT64_T uint64_t ^ ./Include/pyconfig.h:412:9: note: previous definition is here #define PY_UINT64_T unsigned PY_LONG_LONG ^ In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:77:9: warning: 'PY_INT32_T' macro redefined [-Wmacro-redefined] #define PY_INT32_T int32_t ^ ./Include/pyconfig.h:419:9: note: previous definition is here #define PY_INT32_T int ^ In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:78:9: warning: 'PY_INT64_T' macro redefined [-Wmacro-redefined] #define PY_INT64_T int64_t ^ ./Include/pyconfig.h:429:9: note: previous definition is here #define PY_INT64_T PY_LONG_LONG ^ In file included from ./main.go:5: In file included from ./Include/Python.h:83: ./Include/unicodeobject.h:68:2: error: Must define SIZEOF_WCHAR_T #error Must define SIZEOF_WCHAR_T ^ ./Include/unicodeobject.h:71:9: warning: 'Py_UNICODE_SIZE' macro redefined [-Wmacro-redefined] #define Py_UNICODE_SIZE SIZEOF_WCHAR_T ^ ./Include/pyconfig.h:560:9: note: previous definition is here #define Py_UNICODE_SIZE 2 ^ 5 warnings and 1 error generated.

となってしまいました. 
追記へ続く

結局,どこで何を入れれば成功するのでしょうか.
ご教授よろしくお願いいたします.

これ以前の経過も書き記したnoteを以下にリンクしておきます.
https://note.com/clean_camel994/n/nd573d3211451

追記

MacBook-Air ~ % python3 -c "import sys, pprint; pprint.pprint(sys.path)" ['', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/opt/homebrew/lib/python3.10/site-packages']

となったので,これの真ん中を辿って

MacBook-Air / % cd opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10 MacBook-Air 3.10 % ls Headers Resources bin lib Python _CodeSignature include share MacBook-Air 3.10 % cd include MacBook-Air include % ls python3.10 MacBook-Air include % cd python3.10 MacBook-Air python3.10 % ls Python.h genobject.h pyhash.h

となったので,

// #cgo CFLAGS: -I/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/python3.10 // #cgo LDFLAGS: -L/Users/sonoyamayuto/Library/Python/3.9/lib -lpython // #include <Python.h>

としたら先ほどのエラーと変わり,

MacBook-Air sound_wav % go build -o main # sound_wav ld: library not found for -lpython clang: error: linker command failed with exit code 1 (use -v to see invocation)

となりました.

追記2

LDFLAGSの方も修正して

// #cgo CFLAGS: -I/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/python3.10 // #cgo LDFLAGS: -L/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib -lpython3.10 // #include <Python.h>

のようにしたらエラー内容が変わりました.

エラー内容.

MacBook-Air sound_wav % go build -o main # sound_wav Undefined symbols for architecture arm64: "_Pa_CloseStream", referenced from: _main in _x003.o "_Pa_GetDefaultOutputDevice", referenced from: _main in _x003.o "_Pa_GetDeviceInfo", referenced from: _main in _x003.o "_Pa_Initialize", referenced from: _main in _x003.o "_Pa_OpenStream", referenced from: _main in _x003.o "_Pa_StartStream", referenced from: _main in _x003.o "_Pa_StopStream", referenced from: _main in _x003.o "_Pa_Terminate", referenced from: _main in _x003.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

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

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

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

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

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

guest

回答1

0

ベストアンサー

CFLAGSで設定した/Library/Developer/CommandLineTools/usr/bin/python3.9の中に,Python.hはありますか?
多分この/Library/Developer/CommandLineTools/usr/bin/python3.9はPythonの実行ファイルのはずです.
*/bin/の中にバイナリのみならずフォルダやPython.hがあったら驚きです.普通,ヘッダファイルは*/include/,ライブラリは*/lib/の中にあるはずです.ライブラリの設定は当たっているように見えます.

CFLAGSの場所を確認したとおっしゃっていますが,#余談pkg-configを参考に$pkg-config --cflagsで確認されたということで間違い無いでしょうか?

そうでないにしても,弊環境で

shell

1$ python3 -c "import sys, pprint; pprint.pprint(sys.path)"

と実行した結果,

Python

1['', 2 '/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', 3 '/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10', 4 '/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', 5 '/usr/local/lib/python3.10/site-packages', 6 '/usr/local/Cellar/sip/6.7.1/libexec/lib/python3.10/site-packages']

のように出てきたことから,参考サイトのCFLAGSのPATH

C

1// #cgo CFLAGS: -I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9

と比較して

shell

1ls '/usr/local/Cellar/python@3.10/3.10.7/Frameworks/Python.framework/Versions/3.10/include/python3.10/'

のようにして確認したところ,Python.hが存在していました.これを使って参考サイトのようにGOからPythonを実行することができましたよ.

投稿2022/10/07 15:20

編集2022/10/08 03:30
PondVillege

総合スコア1579

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

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

aaaa____

2022/10/08 12:17 編集

>>CFLAGSで設定した/Library/Developer/CommandLineTools/usr/bin/python3.9の中に,Python.hはありますか? ありませんでした. >>CFLAGSの場所を確認したとおっしゃっていますが,#余談pkg-configを参考に$pkg-config --cflagsで確認されたということで間違い無いでしょうか? 申し訳ありません,こちらの確認不足でそのような話があるのを見落としていました. サイトのコードに書いてあったPATHからそれっぽいものを選んでしまいました. brew install して試してみようと思います. サイトでもそうなのですが,PATHでFrameworksにPython.frameworkがあるようなのですが,私の手元のFrameworksでは CoreRepairCore.framework,CoreRepairKit.frameworkしかありません.これはPythonの落とし方が違うとどうしようもないのでしょうか. また,これによって余談の export PKG_CONFIG_PATH=/Library/Frameworks/Python.framework/Versions/3.9/lib/pkgconfig/ にもつまづいてしまっています. 一応インストールしてそのまま何もせずに以下のコマンドをしてみたのですが,こうなりました. MacBook-Air ~ % pkg-config --libs python-3.9-embed Package python-3.9-embed was not found in the pkg-config search path. Perhaps you should add the directory containing `python-3.9-embed.pc' to the PKG_CONFIG_PATH environment variable No package 'python-3.9-embed' found MacBook-Air ~ % pkg-config --cflags python-3.9-embed Package python-3.9-embed was not found in the pkg-config search path. Perhaps you should add the directory containing `python-3.9-embed.pc' to the PKG_CONFIG_PATH environment variable No package 'python-3.9-embed' found
PondVillege

2022/10/08 12:13

そうですね,どうしようもないので,そこは適宜比較して読み替えていただいて,Python.hがある場所まで辿り着くのが良いでしょう.
aaaa____

2022/10/08 12:33

手元で回答にて実行していたものをしてみたのですが,以下のようにPython.frameworkが出てきていて,自分が見ていたFrameworkとは違う場所にあるようでした./Library/Frameworksでした. 自分が見ていたのは FinderではPython.frameworkでした. MacBook-Air ~ % python3 -c "import sys, pprint; pprint.pprint(sys.path)" ['', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/opt/homebrew/lib/python3.10/site-packages'] しかしこれの真ん中を使って // #cgo CFLAGS: -I/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10 // #cgo LDFLAGS: -L/Users/sonoyamayuto/Library/Python/3.9/lib -lpython // #include <Python.h> としたのですが,エラーは相変わらず MacBook-Air sound_wav % go build -o main # sound_wav ./main.go:5:11: fatal error: 'Python.h' file not found #include <Python.h> ^~~~~~~~~~ 1 error generated. となりました.
PondVillege

2022/10/08 12:37

解答でも言っていますが,ヘッダファイルは/lib/の中ではなく/include/の中です.フォルダを辿って適宜読み替える必要があります. /opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/ のフォルダから1つ戻って, /opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/ に行き,そこにincludeフォルダがあるはずなのですがどうでしょう? /opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/ の中を見て更にpython3.10,さらにそのフォルダの中にPython.hがありませんか?
aaaa____

2022/10/08 12:52

理解が遅くて申し訳ございません.おっしゃる通りのところにPython.hがありました. ということは, opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/python3.10 となるわけですね. MacBook-Air / % cd opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10 MacBook-Air 3.10 % ls Headers Resources bin lib Python _CodeSignature include share MacBook-Air 3.10 % cd include MacBook-Air include % ls python3.10 MacBook-Air include % cd python3.10 MacBook-Air python3.10 % ls Python.h genobject.h pyhash.h abstract.h import.h pylifecycle.h bltinmodule.h internal pymacconfig.h ``` // #cgo CFLAGS: -I/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/python3.10 // #cgo LDFLAGS: -L/Users/sonoyamayuto/Library/Python/3.9/lib -lpython // #include <Python.h> ``` と修正して実行してみたところ,次のようになりました. MacBook-Air sound_wav % go build -o main # sound_wav ld: library not found for -lpython clang: error: linker command failed with exit code 1 (use -v to see invocation)
PondVillege

2022/10/08 12:59 編集

CFLAGSに関する問題は解決したみたいですね,LDFLAGSの方も, /opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib -lpython3.10 にしてみたら良いのではないでしょうか?少なくとも,バージョン違いが目立ちます.CFLAGSの方に合わせて実行されるのが良いでしょう.ちなみに,pkgconfigは /opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/pkgconfig/ にありそうですね.このPATHをPKG_CONFIG_PATHとしてpkg-configにて確認することも一手です.
aaaa____

2022/10/09 12:11

LDFLAGSの方もおっしゃる通りにverが合うように修正しましたところ エラー内容が変わりました,PATHについては解決できたようです. アーキテクチャについて言われているようなのですが,arm64では定義されていないと, この問題は解決できるものなのでしょうか.サイトの環境ではmacOSでできているのでM1の自分は問題ないとは思うのですが. ``` MacBook-Air sound_wav % go build -o main # sound_wav Undefined symbols for architecture arm64: "_Pa_CloseStream", referenced from: _main in _x003.o "_Pa_GetDefaultOutputDevice", referenced from: _main in _x003.o "_Pa_GetDeviceInfo", referenced from: _main in _x003.o "_Pa_Initialize", referenced from: _main in _x003.o "_Pa_OpenStream", referenced from: _main in _x003.o "_Pa_StartStream", referenced from: _main in _x003.o "_Pa_StopStream", referenced from: _main in _x003.o "_Pa_Terminate", referenced from: _main in _x003.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) ```
PondVillege

2022/10/09 13:08

時間が経ったものは別途目新しい回答が得られない傾向にありますし,本問題に関しては,質問のタイトルと別問題になりそうなので,自己解決にでもして新しく質問を設置して回答を求めると良いでしょう. その際には本質問からの継続である旨を書いてください.
aaaa____

2022/10/10 11:02

なるほど,ご指摘ありがとうございます, そのようにいたします. ひとまずここまでありがとうございました.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問