python Mecab利用時にエラー「error message: [ifs] no such file or directory:」が発生した場合の解決法

python Mecab利用時にエラー「error message: [ifs] no such file or directory:」が発生した場合の解決法

pythonでMecab利用時にエラー「error message: [ifs] no such file or directory: /usr/local/etc/mecabrc」が発生した場合の解決法を記述してます。

環境

  • OS CentOS 7.9.2009 (Core)
  • Mecab 0.996
  • Python 3.6.8

エラー全文

以下のコードを実行時に発生。

# coding: UTF-8
import MeCab

text = "mecabのテストを行います。"

mecab_obj = MeCab.Tagger("-Ochasen -d /usr/lib64/mecab/dic/mecab-ipadic-neologd/ -t UTF8")
node = mecab_obj.parseToNode(text)
while node:
  print(node.feature)
  node = node.next

エラー

Failed initializing MeCab. Please see the README for possible solutions:

    https://github.com/SamuraiT/mecab-python3#common-issues

If you are still having trouble, please file an issue here, and include the
ERROR DETAILS below:

    https://github.com/SamuraiT/mecab-python3/issues

issueを英語で書く必要はありません。

------------------- ERROR DETAILS ------------------------
arguments: -Ochasen -d /usr/lib64/mecab/dic/mecab-ipadic-neologd/ -t UTF-8
error message: [ifs] no such file or directory: /usr/local/etc/mecabrc
----------------------------------------------------------
Traceback (most recent call last):
  File "mecab.py", line 6, in <module>
    mecab_obj = MeCab.Tagger("-Ochasen -d /usr/lib64/mecab/dic/mecab-ipadic-neologd/ -t UTF-8")
  File "/usr/local/lib64/python3.6/site-packages/MeCab/__init__.py", line 124, in __init__
    super(Tagger, self).__init__(args)
RuntimeError

原因

mecabrcの環境変数が正しく設定されていないため

解決法

環境変数を設定する

sudo find / -iname mecabrc

<出力結果>
/etc/mecabrc

bashに以下を追加します。

sudo vi  ~/.bash_profile

<追加>
export MECABRC='/etc/mecabrc'

反映させます。

source ~/.bash_profile