LoginSignup
40
16

More than 1 year has passed since last update.

[Python3 / pandas] pandasをアップデートしたら、lzmaなるmoduleがimportできないというメッセージが出た

Last updated at Posted at 2020-02-22

発生状況

  1. pyenv で python3.8.1 を新しくインストール
  2. 3.8.1 で virtualenv 環境を構築
  3. $ pip install pandas
    このときに、元々使ってたpandas(0.24.2)よりも新しいバージョン(1.0.1)になった
  4. python のコンソールで import pandas を実行
  5. warning 発生!

Environment

version
CentOS 7.5.1804 (Core)
pyenv pyenv 1.2.8-141-g7097f820
python 3.8.1
pandas 1.0.1

問題のWarning

>>> import pandas
/usr/local/pyenv/versions/20200222/lib/python3.8/site-packages/pandas/compat/__init__.py:117: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
  warnings.warn(msg)

こまった...(´・ω・`)

原因

コチラの記事によると、pandas 0.24.2 なら問題なく動くらしい。

Github issue: import pandas error for missing compression libraries

それ以降のバージョンのpandasを使用する場合は、今回記載している対策が必要になりそう。

もし、どうしても面倒だったら、pandasのバージョンを古いままにしておけば問題を回避(先送り)できるかもしれない...( ˘ω˘)スヤァ(試してないけど)

対策

情報元

[stackoverflow] UserWarning: Could not import the lzma module. Your installed Python is incomplete

対処

方針

lzma-devなるパッケージをOSにインストールした上で、pythonを再インストールする...
えぇ...

実行内容

# [CentOS7] lzma-devインストール
$ sudo yum install -y xz-devel
# [Ubuntu] lzma-devインストール
$ sudo apt-get install liblzma-dev

# pyenv virtualenv の切替え
$ sudo pyenv global [他のvirtualenv環境の名前]

# pyenv virtualenv環境の破棄
$ sudo pyenv unsinstall [作っちゃったvirtualenv環境名]

# python 3.8.1 アンインストール
$ sudo pyenv uninstall 3.8.1

# python 3.8.1 インストールし直し
$ sudo pyenv install 3.8.1

# python 3.8.1 の virtualenv 環境作成
$ sudo pyenv virtualenv 3.8.1 [環境名]

# 今作った virtualenv 環境に切り替える
$ sudo pyenv global [環境名]

# pip module 再インストール
# requirements.txt がない人はモジュールを指定してね
$ sudo pip install -r requirements.txt

では、いざ再テスト!!

$ pip list | grep pandas
pandas             1.0.1

$ python
>>> import pandas
>>>
>>> pandas.DataFrame({})
Empty DataFrame
Columns: []
Index: []

うまくいったぜ!ひゅーひゅー(/・ω・)/

あ....

よく調べてみたら、既に他の方が記事にされてました...orz
両方あると理解が深まる?と信じてリンクを貼っておきます...

【Python3】pandasのversion0.25.0でimportエラーが起きる

追記:Mac M1での対処法

こちらに方法が載っていました
ModuleNotFoundError: No module named '_lzma' #27532

ちなみに私は、実際には以下の操作をしてからpythonをインストールし直すことで、pandasのwarningを解消することができました。

# [MacOS] lzma-devインストール 
% brew install xz

# .zprofile編集
% vim .zprofile

# 以下の内容を .zprofile に追記
prefix=$(brew --prefix)
export LDFLAGS="-L$prefix/opt/xz/lib $LDFLAGS"
export CPPFLAGS="-I$prefix/opt/xz/include $CPPFLAGS"
export PKG_CONFIG_PATH="$prefix/opt/xz/lib/pkgconfig:$PKG_CONFIG_PATH"
# YOU CANNOT HAVE THE GNUBINS in your PATH when you run this
export PYTHON_CONFIGURE_OPTS="--enable-shared"

# 追記した環境変数を有効化する
% source .zprofile

※ただし、これらの環境変数を有効にすると、scipyのインストールがうまくいかなくなります。原因は不明。
なので、

  1. これらの環境変数が有効な状態でpythonを再インストール
  2. .zprofileファイルと環境変数からこれらの変数を削除
  3. pipモジュール(scipy含む)をインストール

という手順を踏みました。
結構手間です。

参考サイト

40
16
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
40
16