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

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

新規登録して質問してみよう
ただいま回答率
85.47%
MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Python

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

Q&A

解決済

1回答

716閲覧

MacOSにおけるPystanのエラーを解決したいです。

kapi0817

総合スコア1

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Python

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

0グッド

0クリップ

投稿2023/04/30 01:45

Pystan上に出てくるErrorを解消したいです。

  • MacOS(13.0(22A380))でPystanを回すとエラーが出ており解消したいです。

前提

MacOS上でAnaconda環境でPystanを使っています。簡単なベイズ推計をするテストコードを回すとエラーが出ております。

発生している問題・エラーメッセージ

DistutilsExecError Traceback (most recent call last) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py:267, in UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 265 linker = compiler_fixup(linker, ld_args) --> 267 self.spawn(linker + ld_args) 268 except DistutilsExecError as msg: File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py:1007, in CCompiler.spawn(self, cmd, **kwargs) 1006 def spawn(self, cmd, **kwargs): -> 1007 spawn(cmd, dry_run=self.dry_run, **kwargs) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/spawn.py:70, in spawn(cmd, search_path, verbose, dry_run, env) 69 cmd = cmd[0] ---> 70 raise DistutilsExecError( 71 "command {!r} failed with exit code {}".format(cmd, exitcode) 72 ) DistutilsExecError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang++' failed with exit code 1 During handling of the above exception, another exception occurred: LinkError Traceback (most recent call last) Cell In[37], line 1 ----> 1 sm = pystan.StanModel(model_code=sample_code,verbose=True) 2 fit = sm.sampling(data=sample_data, iter=1000, chains=4) 3 print(fit) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/pystan/model.py:378, in StanModel.__init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args, allow_undefined, include_dirs, includes) 375 orig_stderr = pystan.misc._redirect_stderr() 377 try: --> 378 build_extension.run() 379 finally: 380 if redirect_stderr: 381 # restore stderr File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:346, in build_ext.run(self) 343 self.compiler.set_link_objects(self.link_objects) 345 # Now actually compile and link everything. --> 346 self.build_extensions() File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:468, in build_ext.build_extensions(self) 466 self._build_extensions_parallel() 467 else: --> 468 self._build_extensions_serial() File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:494, in build_ext._build_extensions_serial(self) 492 for ext in self.extensions: 493 with self._filter_build_errors(ext): --> 494 self.build_extension(ext) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:573, in build_ext.build_extension(self, ext) 570 # Detect target language, if not provided 571 language = ext.language or self.compiler.detect_language(sources) --> 573 self.compiler.link_shared_object( 574 objects, 575 ext_path, 576 libraries=self.get_libraries(ext), 577 library_dirs=ext.library_dirs, 578 runtime_library_dirs=ext.runtime_library_dirs, 579 extra_postargs=extra_args, 580 export_symbols=self.get_export_symbols(ext), 581 debug=self.debug, 582 build_temp=self.build_temp, 583 target_lang=language, 584 ) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py:751, in CCompiler.link_shared_object(self, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 736 def link_shared_object( 737 self, 738 objects, (...) 749 target_lang=None, 750 ): --> 751 self.link( 752 CCompiler.SHARED_OBJECT, 753 objects, 754 output_filename, 755 output_dir, 756 libraries, 757 library_dirs, 758 runtime_library_dirs, 759 export_symbols, 760 debug, 761 extra_preargs, 762 extra_postargs, 763 build_temp, 764 target_lang, 765 ) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py:269, in UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) 267 self.spawn(linker + ld_args) 268 except DistutilsExecError as msg: --> 269 raise LinkError(msg) 270 else: 271 log.debug("skipping %s (up-to-date)", output_filename) LinkError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang++' failed with exit code 1

該当のソースコード

Python3.9.16

1import numpy as np 2import pandas as pd 3import matplotlib.pyplot as plt 4%matplotlib inline 5import seaborn as sns 6sns.set_style('whitegrid') 7 8from sklearn.model_selection import train_test_split 9from sklearn.metrics import mean_squared_error, r2_score 10def generate_sample_data(num, seed=0): 11 target_list = [] # 目的変数のリスト 12 feature_vector_list = [] # 説明変数(特徴量)のリスト 13 14 feature_num = 8 # 特徴量の数 15 intercept = 0.2 # 切片 16 weight = [0.2, 0.3, 0.5, -0.4, 0.1, 0.2, 0.5, -0.3] # 各特徴量の重み 17 18 np.random.seed(seed=seed) 19 for i in range(num): 20 feature_vector = [np.random.rand() for n in range(feature_num)] # 特徴量をランダムに生成 21 noise = [np.random.normal(0, 0.1) for n in range(feature_num)] # ノイズをランダムに生成 22 target = sum([intercept+feature_vector[n]*weight[n]+noise[n] for n in range(feature_num)]) # 目的変数を生成 23 24 target_list.append(target) 25 feature_vector_list.append(feature_vector) 26 27 df = pd.DataFrame(np.c_[target_list, feature_vector_list], 28 columns=['target', 'feature0', 'feature1', 'feature2', 29 'feature3', 'feature4', 'feature5', 'feature6', 'feature7']) 30 return df 31 32data = generate_sample_data(num=1000, seed=0) 33 34X = data.drop('target', axis=1) 35y = data['target'] 36 37X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)] 38import pystan 39import arviz 40sample_code = """ 41 data { 42 int<lower=0> N; 43 int<lower=0> D; 44 matrix[N, D] X; 45 vector[N] y; 46 int<lower=0> N_new; 47 matrix[N_new, D] X_new; 48 } 49 parameters { 50 real w0; 51 vector[D] w; 52 real<lower=0> sigma; 53 } 54 model { 55 for (i in 1:N) 56 y[i] ~ normal(w0 + dot_product(X[i], w), sigma); 57 } 58 generated quantities { 59 vector[N_new] y_new; 60 for (i in 1:N_new) 61 y_new[i] = normal_rng(w0 + dot_product(X_new[i], w), sigma); 62 } 63""" 64 65sample_data = { 66 'N': X_train.shape[0], 67 'D': X_train.shape[1], 68 'X': X_train, 69 'y': y_train, 70 'N_new': X_test.shape[0], 71 'X_new': X_test 72} 73sm = pystan.StanModel(model_code=sample_code,verbose=True) 74fit = sm.sampling(data=sample_data, iter=1000, chains=4) 75print(fit)

試したこと

1.ライブラリのアップデート
2.gccのインストール

Mac上のC++ の処理にエラーが出ていそうなのですが、解決方法がわからず大変困っています。もしご存知の方いらっしゃいましたらお力を貸していただければ幸いです。よろしくお願い致します。

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

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

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

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

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

kapi0817

2023/04/30 01:58 編集

https://discourse.mc-stan.org/t/compiling-error-after-upgrading-to-macos-big-sur/19309/4 こちらの方法を試したのですが、以下のエラーが出てしまっています。 DistutilsExecError Traceback (most recent call last) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py:186, in UnixCCompiler._compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts) 185 try: --> 186 self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs) 187 except DistutilsExecError as msg: File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py:1007, in CCompiler.spawn(self, cmd, **kwargs) 1006 def spawn(self, cmd, **kwargs): -> 1007 spawn(cmd, dry_run=self.dry_run, **kwargs) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/spawn.py:70, in spawn(cmd, search_path, verbose, dry_run, env) 69 cmd = cmd[0] ---> 70 raise DistutilsExecError( 71 "command {!r} failed with exit code {}".format(cmd, exitcode) 72 ) DistutilsExecError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang' failed with exit code 254 During handling of the above exception, another exception occurred: CompileError Traceback (most recent call last) Cell In[42], line 1 ----> 1 sm = pystan.StanModel(model_code=sample_code,verbose=True) 2 fit = sm.sampling(data=sample_data, iter=1000, chains=4) 3 print(fit) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/pystan/model.py:378, in StanModel.__init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args, allow_undefined, include_dirs, includes) 375 orig_stderr = pystan.misc._redirect_stderr() 377 try: --> 378 build_extension.run() 379 finally: 380 if redirect_stderr: 381 # restore stderr File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:346, in build_ext.run(self) 343 self.compiler.set_link_objects(self.link_objects) 345 # Now actually compile and link everything. --> 346 self.build_extensions() File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:468, in build_ext.build_extensions(self) 466 self._build_extensions_parallel() 467 else: --> 468 self._build_extensions_serial() File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:494, in build_ext._build_extensions_serial(self) 492 for ext in self.extensions: 493 with self._filter_build_errors(ext): --> 494 self.build_extension(ext) File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py:549, in build_ext.build_extension(self, ext) 546 for undef in ext.undef_macros: 547 macros.append((undef,)) --> 549 objects = self.compiler.compile( 550 sources, 551 output_dir=self.build_temp, 552 macros=macros, 553 include_dirs=ext.include_dirs, 554 debug=self.debug, 555 extra_postargs=extra_args, 556 depends=ext.depends, 557 ) 559 # XXX outdated variable, kept here in case third-part code 560 # needs it. 561 self._built_objects = objects[:] File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py:599, in CCompiler.compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends) 597 except KeyError: 598 continue --> 599 self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) 601 # Return *all* object filenames, not just the ones we just built. 602 return objects File ~/opt/anaconda3/envs/MyEnvitonment/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py:188, in UnixCCompiler._compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts) 186 self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs) 187 except DistutilsExecError as msg: --> 188 raise CompileError(msg) CompileError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang' failed with exit code 254
jbpb0

2023/04/30 10:53

別の質問(削除リクエストがされてるので今は参照できない)に、たしかmacのcpuがarm(m1かm2)と書かれてたような気がしますが、この質問の対象のmacもそうでしょうか?
kapi0817

2023/04/30 12:08

ご返信頂きありがとうございます。 そうです。M2のMacを使用しております。
jbpb0

2023/04/30 12:34

> CompileError: command '/Users/username/opt/anaconda3/envs/MyEnvitonment/bin/x86_64-apple-darwin13.4.0-clang' failed with exit code 254 コンパイラのファイル名に付いてる「x86_64」は、インテルcpu用という意味だと思います macのcpuがarmなのに、インテルcpu用のコンパイラを使ってることが、エラーの原因ではないですかね
kapi0817

2023/04/30 12:59

ありがとうございます! 素人質問で大変恐縮ですが、gccのインストールが原因かと思ってるのですが、こちらのコンパイラを変更するにはどうすればよろしいかご存じでしょうか。
jbpb0

2023/04/30 13:14

gccはどのようにしてインストールしたのでしょうか?
kapi0817

2023/04/30 13:20

brew install gccでインストール致しました。
jbpb0

2023/04/30 13:46

> brew install gccでインストール致しました。 その「brew」は、インテル用のではなくて、arm用のでしょうか? また、エラーメッセージによると、 /Users/username/opt/anaconda3/envs/MyEnvitonment/bin に x86_64-apple-darwin13.4.0-clang++ があるようです /Users/username/opt/anaconda3 は「anaconda」がインストールされてる場所なので、おそらく「brew」でインストールしたものはそこには置かれないと思うのですが、「MyEnvitonment」という仮想環境に「conda」か「pip」でgcc関連のものを何かインストールしてませんでしょうか? ターミナルで仮想環境「MyEnvitonment」を有効にした状態で下記を実行したら、何か引っかかりませんでしょうか? conda list | grep gcc conda list | grep clang pip list | grep gcc pip list | grep clang
kapi0817

2023/04/30 13:59

すみません、色々とご迷惑をおかけし申し訳ないです、、。色々と試した際に condaで入れてしまったようです。 conda list | grep clang で clang 10.0.0 default_hf57f61e_0 clang_osx-64 10.0.0 h05bbb7f_0 anaconda clangxx 10.0.0 default_hf57f61e_0 clangxx_osx-64 10.0.0 h05bbb7f_1 anaconda libclang 14.0.6 default_hd95374b_1 libclang13 14.0.6 default_habbcc1a_1 上記のものが出て参りました。
kapi0817

2023/04/30 14:27

そうです!M1用をインストール致しました。
jbpb0

2023/05/08 03:57 編集

> clang_osx-64 10.0.0 h05bbb7f_0 anaconda > clangxx_osx-64 10.0.0 h05bbb7f_1 anaconda は、「osx-64」が付くインテルcpu用の https://anaconda.org/anaconda/clang_osx-64 https://anaconda.org/anaconda/clangxx_osx-64 がインストールされてますが、「osx-arm64」が付くarm cpu用の https://anaconda.org/anaconda/clang_osx-arm64 https://anaconda.org/anaconda/clangxx_osx-arm64 をインストールしないといけないのだと思います > clang 10.0.0 default_hf57f61e_0 > clangxx 10.0.0 default_hf57f61e_0 > libclang 14.0.6 default_hd95374b_1 > libclang13 14.0.6 default_habbcc1a_1 は、 https://anaconda.org/main/clang/files?sort=basename&sort_order=asc https://anaconda.org/main/clangxx/files?sort=basename&sort_order=asc https://anaconda.org/main/libclang/files?sort=basename&sort_order=asc https://anaconda.org/main/libclang13/files?sort=basename&sort_order=asc を見ると、「osx-64」が付くインテルcpu用のがインストールされてますが、「osx-arm64」が付くarm cpu用のをインストールしないといけないのだと思います インテルcpu用のを全てアンインストールして、arm cpu用のをインストールすれば直るかもしれませんが、上記以外にもインテルcpu用のが何か入ってるかもしれないので、anacondaのインストールからやり直した方がいいかもしれません (arm用のanacondaに、インテルcpu用のが入ってるのは、謎ですが)
kapi0817

2023/05/08 09:16

ご丁寧に教えていただきありがとうございます! アンインストールとarm用の再インストールを試してみます!
guest

回答1

0

自己解決

jgpb0様のarm用のanaconda再インストールで問題が解決いたしました。
この度はご協力いただきありがとうございます。素人質問ばかりで大変恐縮ですが、優しく色々教えていただきありがとうございました。引き続きプログラミングの勉強を頑張っていきます。

投稿2023/05/13 08:17

kapi0817

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問