--macOS Big Sur version 11.0.1 --Install python with pyenv
When I casually tried to import pandas with python,
import pandas as pd
I got the following error:
Traceback (most recent call last):
...(Omission)
ImportError: No module named '_bz2'
When you install python with pyenv, you can expect that the bz2 library for python is not installed and is incomplete because the compiler did not recognize the package bzip2.
So, when I installed it with pyenv, I would have seen the following warning.
$ pyenv install 3.9.0
...(Omission)
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
With the compiler aware of the bzip2 library, use pyenv to reinstall python.
$ brew install bzip2
During the installation, you will probably get the following message: This is very important.
For compilers to find bzip2 you may need to set:
export LDFLAGS="-L/usr/local/opt/bzip2/lib"
export CPPFLAGS="-I/usr/local/opt/bzip2/include"
If you just install it with brew, the compiler will not recognize the bzip2 package when building python with pyenv.
In order for the compiler to recognize it, you need to set LDFLAGS
and CPPFLAGS
as in the message that came out earlier.
To do this, add the following statement inside your .zprofile or .bash_profile:
export LDFLAGS="-L/usr/local/opt/bzip2/lib"
export CPPFLAGS="-I/usr/local/opt/bzip2/include"
If you had previously set different LDFLAGS
and CPPFLAGS
, you can specify more than one of these FLAGs by connecting them with a space, so you can do the following:
export LDFLAGS="$LDFLAGS -L/usr/local/opt/bzip2/lib"
export CPPFLAGS="$CPPFLAGS -I/usr/local/opt/bzip2/include"
Install the version of your choice with pyenv.
$ pyenv install <version>
Perhaps the previous warning has disappeared.
After that, follow the usual pyenv installation procedure, specify the main python version to use, and reload the shell.
$ pyenv global <The version you just installed>
$ exec $SHELL -l
You should probably be able to load pandas.
The following articles will be helpful. Problem with no module named'_bz2' in scikit-learn
I found many ways to deal with the same error on Linux, but I couldn't find a way to deal with the error on macOS, so I had to solve it myself.
Recommended Posts