pip install mecab-python3 が失敗する

腹立つなァ....

検索してみるとみんな苦労しているようだが、
エラー内容もその対応も様々。



# pip install mecab-python3
Collecting mecab-python3
  Using cached https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz
Installing collected packages: mecab-python3
  Running setup.py install for mecab-python3 ... error
    Complete output from command /usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-hzcdo768/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 /tmp/pip-record-5w7so2s3/install-record.txt --single-version-externally-managed --compile:
    /usr/lib64/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_py
    running build_ext
    building '_MeCab' extension
    swigging MeCab.i to MeCab_wrap.cpp
    swig -python -shadow -c++ -I/usr/local/include -o MeCab_wrap.cpp MeCab.i
    /usr/local/include/mecab.h:848: Warning(302): Identifier 'set_sentence' redefined by %extend (ignored),
    MeCab.i:88: Warning(302): %extend definition of 'set_sentence'.
    creating build
    creating build/temp.linux-x86_64-3.6
    gcc -pthread -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/local/include -I/usr/include/python3.6m -c MeCab_wrap.cpp -o build/temp.linux-x86_64-3.6/MeCab_wrap.o
    MeCab_wrap.cpp: In function ‘swig_module_info* SWIG_Python_GetModule()’:
    MeCab_wrap.cpp:2452: error: ‘PyCObject_Import’ was not declared in this scope
    MeCab_wrap.cpp: In function ‘void SWIG_Python_SetModule(swig_module_info*)’:
    MeCab_wrap.cpp:2521: error: ‘PyCObject_FromVoidPtr’ was not declared in this scope
    MeCab_wrap.cpp:2512: 警告: unused variable ‘swig_empty_runtime_method_table’
    MeCab_wrap.cpp: In function ‘swig_type_info* SWIG_Python_TypeQuery(const char*)’:
    MeCab_wrap.cpp:2544: error: ‘PyCObject_AsVoidPtr’ was not declared in this scope
    MeCab_wrap.cpp:2549: error: ‘PyCObject_FromVoidPtr’ was not declared in this scope
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-hzcdo768/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 /tmp/pip-record-5w7so2s3/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-hzcdo768/mecab-python3/


ログを見てみる。

#### # pip install mecab-python3

実行したコマンド。
コマンドがないとかいうエラーはない。

#### Collecting mecab-python3
####
####   Using cached https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz


このtar.gzファイルをダウンロードしてインストールしているようだ。

#### Installing collected packages: mecab-python3
####
####   Running setup.py install for mecab-python3 ... error

で、setup.pyが error になっている。




####     Complete output from command /usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-hzcdo768/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 /tmp/pip-record-5w7so2s3/install-record.txt --single-version-externally-managed --compile:

以下が setup.pyのエラーの詳細かな。

####
####     /usr/lib64/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'

これはワーニング

####
####       warnings.warn(msg)
####
####     running install
####
####     running build
####
####     running build_py
####
####     running build_ext
####
####     building '_MeCab' extension
####
####     swigging MeCab.i to MeCab_wrap.cpp
####
####     swig -python -shadow -c++ -I/usr/local/include -o MeCab_wrap.cpp MeCab.i


「swigがインストールされていない」という人がいたが、swigはインストールされている。

####
####     /usr/local/include/mecab.h:848: Warning(302): Identifier 'set_sentence' redefined by %extend (ignored),

これもワーニング

####
####     MeCab.i:88: Warning(302): %extend definition of 'set_sentence'.

これもワーニング

####
####     creating build
####

buildを作成

####     creating build/temp.linux-x86_64-3.6
####
####     gcc -pthread -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/local/include -I/usr/include/python3.6m -c MeCab_wrap.cpp -o build/temp.linux-x86_64-3.6/MeCab_wrap.o

gccを呼び出し

####
####     MeCab_wrap.cpp: In function ‘swig_module_info* SWIG_Python_GetModule()’:
####
####     MeCab_wrap.cpp:2452: error: ‘PyCObject_Import’ was not declared in this scope


エラー。PyCObject_Import が宣言されていない。

####
####     MeCab_wrap.cpp: In function ‘void SWIG_Python_SetModule(swig_module_info*)’:
####
####     MeCab_wrap.cpp:2521: error: ‘PyCObject_FromVoidPtr’ was not declared in this scope

エラー。PyCObject_FromVoidPtr が宣言されていない。

####
####     MeCab_wrap.cpp:2512: 警告: unused variable ‘swig_empty_runtime_method_table’

警告。未使用の変数。

####
####     MeCab_wrap.cpp: In function ‘swig_type_info* SWIG_Python_TypeQuery(const char*)’:
####
####     MeCab_wrap.cpp:2544: error: ‘PyCObject_AsVoidPtr’ was not declared in this scope

エラー。PyCObject_FromVoidPtr が宣言されていない。

####
####     MeCab_wrap.cpp:2549: error: ‘PyCObject_FromVoidPtr’ was not declared in this scope

エラー。PyCObject_FromVoidPtr が宣言されていない。

####
####     error: command 'gcc' failed with exit status 1

gccが失敗した。

####
####
####
####     ----------------------------------------
####
#### Command "/usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-hzcdo768/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 /tmp/pip-record-5w7so2s3/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-hzcdo768/mecab-python3/
####
####
####

違うエラーなのだが、「setup.pyがpython3に対応していない」という人がいる。
多分そんなところだろう。

setup.pyはどこにあるのか。
探すと...

/home/monqy/mecab/mecab/python/setup.py

これはmecab本体かな。

/home/monqy/mecab-python-0.996/setup.py

こっちか。

setup.pyを実行してみるとエラーになる。オプションが必要のようだ。

ログをもう一度見てみると

Complete output from command
/usr/bin/python3.6 -u -c "import setuptools, tokenize;

__file__='/tmp/pip-install-hzcdo768/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 /tmp/pip-record-5w7so2s3/install-record.txt
--single-version-externally-managed
--compile:

多分、最初の方にあるURLからファイルをダウンロード、/tmpに展開し、
そこのsetup.pyを実行している。

/tmp/pip-install-hzcdo768/mecab-python3/

というフォルダはない。
インストールに失敗した後に消しているのだろう。


よーし、では、tar.gzを持ってきて、解凍→インストールしてみよう。

wget https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz


ダメ。

swigのバージョンが古いのだろうか。

# swig -version

SWIG Version 1.3.40


でもswigの新しいバージョンの入れ方がわからない。

twitterで、ある人がバージョン指定すればいけると。

pip install mecab-python3==0.7

...いけた。

# pip install mecab-python3==0.7
Collecting mecab-python3==0.7
  Downloading https://files.pythonhosted.org/packages/25/e9/bbf5fc790a2bedd96fbaf47a84afa060bfb0b3e0217e5f64b32bd4bbad69/mecab-python3-0.7.tar.gz (41kB)
    100% |????????????????????????????????| 51kB 2.4MB/s
Installing collected packages: mecab-python3
  Running setup.py install for mecab-python3 ... done
Successfully installed mecab-python3-0.7


とりあえずpythonからmecabが使えるようになった。

ふう。