[PYTHON] When I try to import pandas on macOS I get the error No module named'_bz2'

environment

--macOS Big Sur version 11.0.1 --Install python with pyenv

problem

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'

Cause

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?

Solutions

With the compiler aware of the bzip2 library, use pyenv to reinstall python.

1. Install the bzip2 package

$ 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"

2. Set LDFLAGS and CPPFLAGS

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"

3. Reinstall python

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.

(Reference) For Linux

The following articles will be helpful. Problem with no module named'_bz2' in scikit-learn

Impressions

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

When I try to import pandas on macOS I get the error No module named'_bz2'
When I import TensorFlow to Python, I get "Import Error: DLL load failed: The specified module cannot be found."
I get [Error 2055] when trying to connect to MySQL on Heroku
I get an error when I try to raise Python to 3 series using pyenv on Catalina
I get an error with import pandas.
When I try to update the data on DynamoDB with Python (boto3), I get "ExpressionAttributeNames contains invalid key: Syntax error; key: <key name>"
I get a Python No module named'encodings' error with the aws command
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
The problem that scikit-learn gives the error No module named'_bz2'
Every time I try to read a csv file using pandas, I get a numpy error.
When I try to install mysqlclient with Django, I get error: command'gcc' failed with exit status 1.
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do if you get Swagger-codegen in python and Import Error: No module named
When I try to connect to MySQL with mysql-connector-python, I cannot connect with the error "SSL connection error: SSL_CTX_set_tmp_dh failed"
I got an error when I tried to process luigi in parallel on windows, but the solution
I get "sanity check" and "No module" errors in import numpy
The record I was addicted to when putting MeCab on Heroku
I tried to get started with Bitcoin Systre on the weekend
When I try to use pip, SSL module is not available.
What to do when you get "I can't see the site !!!!"
Error when installing opencv-python on jetson nano (ModuleNotFoundError: No module named'skbuild')
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
What to do when "OS Error: [WinError 126] The specified module cannot be found" appears in import torch
When I try to upgrade pip, I get an infinite loop after failing to upgrade
Error resolution when installing numba on macOS
When I get an error with PyInstaller
Import Error in Python3: No module named'xxxxx'
I get an error ~ is zero, singular U when passing the covariance matrix from the Linear layer to MultivariateNormal
In the Chainer tutorial, I get an error when importing a package. (mock)
I stumbled on the character code when converting CSV to JSON in Python
Attempting to install opencv-python3 on jetson nano but error (ModuleNotFoundError: No module named'skbuild')
When I try to use Jupiter notebook on Mac, I can only select python2
I want to be notified when the command operation is completed on linux!
When I try to connect django and postgresql with Docker, I get the error "django.db.utils.OperationalError: could not translate host name" db "to address: Name or service not known"
Solution for "Import Error: No module named requests"
ModuleNotFoundError: No module named'_bz2' error in pyenv Python
I tried to understand how to use Pandas and multicollinearity based on the Affairs dataset.
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
How to deal with "Type Error: No matching signature found" error when using pandas fillna
When I deployed the Django app to Heroku, I got Module Not Found: <project-name> .wsgi.
What to do if you get an Import Error when importing matplotlib with Jupyter
I got an error when pip install pandas on Mac, so I dealt with it
I get an Import Error in Python Beautiful Soup
Try to estimate the number of likes on Twitter
I get an error when I put opencv in pyautoGUI
Try to get the contents of Word with Golang
I get an error when trying meinheld + WebSocket + mongodb
I get "too early to create image" on tkinter
I tried to notify the honeypot report on LINE
I got an error when pip install tweepy on macOS Sierra, so I dealt with it
What to do when you get an error saying "Name resolution temporarily failed" on linux
When I run pipenv install <package name>, I get a "No matching distribution found for <package name>" error.
When I installed python on macOS and used it, I got an error when I put an https connection
[Solution] When I try to connect to CloudSQL with GAE, I get an ImportError only when using dev_appserver.
What to do if you get an Undefined error:'Module_six_moves_urllib_parse' object has no attribute'urlencode' on MacOS