pandasでbz2関係でエラーになった話

今日は急に寒くなりましたが、いかがお過ごしでしょうか?
私はXserverにPythonをソースからインスコして遊んでいる今日この頃です。
データサイエンティストっぽいことをしようとpandasをインストールしたのですが、インストール時にsuccessと出た割には、importしたらbz2エラーになってしまいました。

原因はわかっていて、Pythonをmakeした時にメッセージに出てたのを無視したからです。
というわけで、いつものごとくbzip2をインストールしてからPythonを再インストールします。

bzip2をインストールする。

homeディレクトリにlocalディレクトリを作っておいて、そこを作業場兼バイナリ置き場にしておきます。

cd local/

wget https://sourceware.org/pub/bzip2/bzip2-latest.tar.gz
tar xvfx bzip2-latest.tar.gz 
cd bzip2-1.0.8/

Makefileの18行目(bzip2バージョン1.0.8)に-fPICを追加します、これやらないと再コンパイルしろって言ってくる。よく分からない。

CC=gcc -fPIC
make -f Makefile-libbz2_so
make install PREFIX=$HOME/local/bzip2
# libbz2.aが吐き出されるけど、soも必要っぽい?
mv bzip2-1.0.8/libbz2.so.1.0.8 bzip2/lib/libbz2.so.1.0.8

.bash_profileに追加。
色々無駄な行を追加したかもしれない。

# bzip2 settings
export PATH=$HOME/local/bzip2/bin:$PATH
export LD_LIBRARY_PATH=$HOME/local/bzip2/lib:$LD_LIBRARY_PATH
export LDFLAGS="$LDFLAGS -L $HOME/local/bzip2/lib"
export CPPFLAGS="$CPPFLAGS -I $HOME/local/bzip2/include"
export CFLAGS="-fPIC"

bash_profileを再読み込み。

source ./bash_profile

Pythonを再インストールする。

ffiとopensslについては前と同じ。

./configure --prefix=$HOME/local/python/ --with-system-ffi LDFLAGS="-L $HOME/local/lib64/:$HOME/local/lib/" CPPFLAGS="-I $HOME/local/include/" --with-openssl=$HOME/local/openssl/ --with-openssl-rpath=auto
make
make install

テスト。

(myenv-3.10.6) [xsXXXXXX@svXXXXX ~]$ local/python/bin/python3 -m venv myenv-3.10.6
(myenv-3.10.6) [xsXXXXXX@svXXXXX ~]$ source myenv-3.10.6/bin/activate
(myenv-3.10.6) [xsXXXXXX@svXXXXX ~]$ python
Python 3.10.6 (main, Oct 24 2022, 15:48:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> 

いけた。

参考にしたもののリンク

偉大なる先輩方、ありがとう。

https://qiita.com/euphoria108/items/8bced0f3d5a8ad9ce9d2

https://stackoverflow.com/questions/63326781/usr-local-lib-libbz2-a-could-not-read-symbols-bad-value

http://justanyone.blogspot.com/2012/07/compiling-python-with-bz2.html

https://qiita.com/euphoria108/items/8bced0f3d5a8ad9ce9d2

余談

fPICしないとこんなエラーが出る。

building '_bz2' extension
gcc -pthread -fPIC -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -fPIC -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I./Include -I/home/xsXXXXXX/local/python/include -I. -I/home/xsXXXXXX/local/include/ -I/home/xsXXXXXX/local/bzip2/include -I/usr/local/include -I/home/xsXXXXXX/Python-3.10.6/Include -I/home/xsXXXXXX/Python-3.10.6 -c /home/xsXXXXXX/Python-3.10.6/Modules/_bz2module.c -o build/temp.linux-x86_64-3.10/home/xsXXXXXX/Python-3.10.6/Modules/_bz2module.o
gcc -pthread -shared -L /home/xsXXXXXX/local/lib64/:/home/xsXXXXXX/local/lib/ -L /home/xsXXXXXX/local/bzip2/lib -L /home/xsXXXXXX/local/bzip2/lib -L /home/xsXXXXXX/local/bzip2/lib -L /home/xsXXXXXX/local/bzip2/lib -fPIC -I /home/xsXXXXXX/local/bzip2/include -I /home/xsXXXXXX/local/bzip2/include build/temp.linux-x86_64-3.10/home/xsXXXXXX/Python-3.10.6/Modules/_bz2module.o -L/home/xsXXXXXX/local/python/lib -L/home/xsXXXXXX/local/bzip2/lib -L/usr/local/lib -lbz2 -o build/lib.linux-x86_64-3.10/_bz2.cpython-310-x86_64-linux-gnu.so
/usr/bin/ld: /home/xsXXXXXX/local/bzip2/lib/libbz2.a(bzlib.o): relocation R_X86_64_32S against symbol `BZ2_crc32Table' can not be used when making a shared object。 -fPIC を付けて再コンパイルしてください。
/usr/bin/ld: /home/xsXXXXXX/local/bzip2/lib/libbz2.a(compress.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object。 -fPIC を付けて再コンパイルしてください。
/usr/bin/ld: /home/xsXXXXXX/local/bzip2/lib/libbz2.a(decompress.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object。 -fPIC を付けて再コンパイルしてください。
/usr/bin/ld: /home/xsXXXXXX/local/bzip2/lib/libbz2.a(blocksort.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object。 -fPIC を付けて再コンパイルしてください。
/usr/bin/ld: 最終リンクに失敗しました: 出力に対応するセクションがありません
collect2: エラー: ld はステータス 1 で終了しました

The necessary bits to build these optional modules were not found:
_curses               _curses_panel         _sqlite3           
_tkinter              _uuid                 readline           
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  pwd                   time               


Failed to build these modules:
_bz2                                                           

そのうちSQLiteも入れる必要あるのかなぁ。。。

コメントする