11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

macでmecab-python3のimport error(Symbol not found)を解決

Last updated at Posted at 2021-06-27

環境
MAC OS 11.3.1 (M1)
HOMEBREW_VERSION: 3.2.0
mecab: stable 0.996 (bottled)
swig: stable 4.0.2 (bottled)
pyenv 1.2.26
python 3.9.4
pip 21.1.3
mecab-python3 Version: 1.0.4

発生した問題

importしてmecabを使おうとしたところ・・・

import MeCab
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/__init__.py", line 10, in <module>
    from . import _MeCab
ImportError: dlopen(/Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so, 2): Symbol not found: __ZN5MeCab11createModelEPKc
  Referenced from: /Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so
  Expected in: flat namespace
 in /Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so

Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so
を実行する際に
Symbol not found: __ZN5MeCab11createModelEPKcというエラーが発生。

otool -Lで依存している共有ライブラリの名前とバージョンを確認する

G1998G@MyMac% otool -L /Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so
/Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so:
	/usr/lib/libmecab.dylib (compatibility version 1.0.0, current version 929.9.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 905.6.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)

ls /usr/lib/したところ、上記3つのdylibファイルは存在しなかった。
探してみたところ私の場合は /opt/homebrew/lib/というディレクトリに上記が存在した。

install_name_tool -changeでパスを置換

3つの.dylibのうち、一番上の/usr/lib/libmecab.dylib
正しいパスの/opt/homebrew/lib/libmecab.dylibに置換する。
install_name_tool -changeを使用。

G1998G@MyMac%  install_name_tool -change "/usr/lib/libmecab.dylib" /opt/homebrew/lib/libmecab.dylib /Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so

otool -Lで置換されているか確認。

G1998G@MyMac% otool -L /Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so
/Users/G1998G/.pyenv/versions/3.9.4/lib/python3.9/site-packages/MeCab/_MeCab.cpython-39-darwin.so:
	/opt/homebrew/lib/libmecab.dylib (compatibility version 1.0.0, current version 929.9.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 905.6.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)

置換成功。
残り2つの.dylibも同様に対処したところ正常にimportすることができました。

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?