【Python】mecab-python3を使えるようにするまで

以下のような環境を用意しようとしたのですが、

version: "3"
services:
  app:
    build:
      context: .
      dockerfile: ./Dockerfile
    tty: true
FROM python:3.12

RUN pip install --no-cache-dir -r requirements.txt
mecab-python3==1.0.8

スポンサードサーチ

エラー1

docker compose build の実行中に以下のようなエラーが出ました。

0 10.55   Building wheel for ipadic (setup.py): started
#0 12.44   Building wheel for ipadic (setup.py): finished with status 'done'
#0 12.45   Created wheel for ipadic: filename=ipadic-1.0.0-py3-none-any.whl size=13556704 sha256=0ba5af5029f870abb51210127b922cdb3b909fde70c48d671ea57478c5dee4bc
#0 12.45   Stored in directory: /tmp/pip-ephem-wheel-cache-e3z0cnq7/wheels/93/8b/55/dd5978a069678c372520847cf84ba2ec539cb41917c00a2206
#0 12.45   Building wheel for mecab-python3 (setup.py): started
#0 12.58   Building wheel for mecab-python3 (setup.py): finished with status 'error'
#0 12.58   error: subprocess-exited-with-error
#0 12.58
#0 12.58   × python setup.py bdist_wheel did not run successfully.
#0 12.58   │ exit code: 1
#0 12.58   ╰─> [21 lines of output]
#0 12.58       /usr/local/lib/python3.12/site-packages/setuptools/__init__.py:80: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
#0 12.58       !!
#0 12.58
#0 12.58               ********************************************************************************
#0 12.58               Requirements should be satisfied by a PEP 517 installer.
#0 12.58               If you are using pip, you can try `pip install --use-pep517`.
#0 12.58               ********************************************************************************
#0 12.58
#0 12.58       !!
#0 12.58         dist.fetch_build_eggs(dist.setup_requires)
#0 12.58       WARNING setuptools_scm.pyproject_reading toml section missing 'pyproject.toml does not contain a tool.setuptools_scm section'
#0 12.58       running bdist_wheel
#0 12.58       running build
#0 12.58       running build_py
#0 12.58       creating build
#0 12.58       creating build/lib.linux-aarch64-cpython-312
#0 12.58       creating build/lib.linux-aarch64-cpython-312/MeCab
#0 12.58       copying src/MeCab/__init__.py -> build/lib.linux-aarch64-cpython-312/MeCab
#0 12.58       copying src/MeCab/cli.py -> build/lib.linux-aarch64-cpython-312/MeCab
#0 12.58       running build_ext
#0 12.58       error: [Errno 2] No such file or directory: 'mecab-config'
#0 12.58       [end of output]
#0 12.58
#0 12.58   note: This error originates from a subprocess, and is likely not a problem with pip.
#0 12.59   ERROR: Failed building wheel for mecab-python3
#0 12.59   Running setup.py clean for mecab-python3
#0 12.69 Successfully built ipadic
#0 12.69 Failed to build mecab-python3
#0 12.69 ERROR: Could not build wheels for mecab-python3, which is required to install pyproject.toml-based projects
#0 12.79
#0 12.79 [notice] A new release of pip is available: 23.2.1 -> 23.3.1
#0 12.79 [notice] To update, run: pip install --upgrade pip
------
failed to solve: process "/bin/sh -c pip install --no-cache-dir -r requirements.txt" did not complete successfully: exit code: 1

これはPythonのバージョンを3.11にしたことで解決。
(2023年12月初頭時点)

FROM python:3.11

RUN pip install --no-cache-dir -r requirements.txt

エラー2

以下のようなpyファイルを用意し、

import MeCab

sentence = "庭には二羽鶏がいる"
tagger = MeCab.Tagger()
print(tagger.parse(sentence))

実行すると以下のように/usr/local/etc/mecabrcがないよ。っとエラーが発生した

----------------------------------------------------------

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:
default dictionary path: None
[ifs] no such file or directory: /usr/local/etc/mecabrc
----------------------------------------------------------

個人的にはipadicを使いたかったので、以下のように対応

+ ipadic==1.0.0
mecab-python3==1.0.8
+ impoert ipadic
import MeCab

sentence = "庭には二羽鶏がいる"
+ tagger = MeCab.Tagger(ipadic.MECAB_ARGS)
- tagger = MeCab.Tagger()
print(tagger.parse(sentence))

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です