JupyterLabでMeCabを実行するとエラーが出る

JupyterLabを利用して、MeCabを実行したいのですがうまく環境構築ができずに困っています。

MacOSのターミナル上では実行できるようになり、MeCabコマンド単体での実行やPythonファイルからのMeCabコマンドも実行できています。
JupyterLab上だけエラーが出て実行できません。

実行したいコード

# # 形態素解析エンジン「MeCab」テスト # import MeCab wakati = MeCab.Tagger("-Owakati -r /usr/local/etc/mecabrc -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd/") result = wakati.parse('こんにちは。これはテストです。').split() print(result)

エラー内容(JupyterLab上で実行した場合のみ下記エラーが出る)

--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-23-13b41c8a508c> in <module> 3 # 4 ----> 5 import MeCab 6 7 wakati = MeCab.Tagger("-Owakati -r /usr/local/etc/mecabrc -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd/") ModuleNotFoundError: No module named 'MeCab'

mecab-python3をJupyterLab上でインストールしなければいけないと思い、pipを実行すると

!pip install mecab-python3

次のような実行結果に。エラー。

Collecting mecab-python3 Using cached mecab-python3-1.0.5.tar.gz (77 kB) Building wheels for collected packages: mecab-python3 Building wheel for mecab-python3 (setup.py) ... error ERROR: Command errored out with exit status 1: command: /Users/hiro/opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/setup.py'"'"'; __file__='"'"'/private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-wheel-i8pnc21m cwd: /private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/ Complete output (10 lines): running bdist_wheel running build running build_py creating build creating build/lib.macosx-10.9-x86_64-3.8 creating build/lib.macosx-10.9-x86_64-3.8/MeCab copying src/MeCab/__init__.py -> build/lib.macosx-10.9-x86_64-3.8/MeCab copying src/MeCab/cli.py -> build/lib.macosx-10.9-x86_64-3.8/MeCab running build_ext error: [Errno 2] No such file or directory: 'mecab-config' ---------------------------------------- ERROR: Failed building wheel for mecab-python3 Running setup.py clean for mecab-python3 Failed to build mecab-python3 Installing collected packages: mecab-python3 Running setup.py install for mecab-python3 ... error ERROR: Command errored out with exit status 1: command: /Users/hiro/opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/setup.py'"'"'; __file__='"'"'/private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-record-orl3eh_2/install-record.txt --single-version-externally-managed --compile --install-headers /Users/hiro/opt/anaconda3/include/python3.8/mecab-python3 cwd: /private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/ Complete output (10 lines): running install running build running build_py creating build creating build/lib.macosx-10.9-x86_64-3.8 creating build/lib.macosx-10.9-x86_64-3.8/MeCab copying src/MeCab/__init__.py -> build/lib.macosx-10.9-x86_64-3.8/MeCab copying src/MeCab/cli.py -> build/lib.macosx-10.9-x86_64-3.8/MeCab running build_ext error: [Errno 2] No such file or directory: 'mecab-config' ---------------------------------------- ERROR: Command errored out with exit status 1: /Users/hiro/opt/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/setup.py'"'"'; __file__='"'"'/private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-install-gaa7ujo5/mecab-python3/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/mw/14kwc1c55td4n29409r12zg80000gn/T/pip-record-orl3eh_2/install-record.txt --single-version-externally-managed --compile --install-headers /Users/hiro/opt/anaconda3/include/python3.8/mecab-python3 Check the logs for full command output.

「mecab-config」ディレクトリが見つからないと出るのですが、解決方法を調べてもさっぱりわかりません。
アドバイスを頂けますと幸いです。

実行環境:
MacOS v10.12.6
JupyterLab v2.2.6

(以下、ターミナル上で確認)
Python v3.9.2
mecab v0.996
mecab-python3 v1.0.5

コメントを投稿

0 コメント