记录一个可能会再次遇到又容易忘记的灯下黑的问题备查,以免以后再遇到浪费时间。
安装模型的支持环境时可能有时会遇到ModuleNotFoundError: No module named '_bz2'这样问题,比如说yolov5第六版的export脚本所需的pandas包就需要bzip2的支持,可能会报下面这样的错误:
Traceback (most recent call last):
File "export.py", line 40, in
from models.common import Conv
File "/workspace/yolov5-v6/models/common.py", line 13, in
import pandas as pd
File "/usr/local/lib/python3.6/site-packages/pandas/__init__.py", line 52, in
from pandas.core.api import (
File "/usr/local/lib/python3.6/site-packages/pandas/core/api.py", line 29, in
from pandas.core.groupby import Grouper, NamedAgg
File "/usr/local/lib/python3.6/site-packages/pandas/core/groupby/__init__.py", line 1, in
from pandas.core.groupby.generic import DataFrameGroupBy, NamedAgg, SeriesGroupBy
File "/usr/local/lib/python3.6/site-packages/pandas/core/groupby/generic.py", line 57, in
from pandas.core.aggregation import (
File "/usr/local/lib/python3.6/site-packages/pandas/core/aggregation.py", line 27, in
from pandas.core.series import FrameOrSeriesUnion, Series
File "/usr/local/lib/python3.6/site-packages/pandas/core/series.py", line 68, in
from pandas.core import algorithms, base, generic, nanops, ops
File "/usr/local/lib/python3.6/site-packages/pandas/core/generic.py", line 102, in
from pandas.io.formats import format as fmt
File "/usr/local/lib/python3.6/site-packages/pandas/io/formats/format.py", line 71, in
from pandas.io.common import stringify_path
File "/usr/local/lib/python3.6/site-packages/pandas/io/common.py", line 3, in
import bz2
File "/usr/local/lib/python3.6/bz2.py", line 23, in
from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
这时去安装bzip2,却发现bzip2已经安装了:
查对应的so文件也确实存在:
/usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
怎么就是报错呢?开始没仔细看环境,没注意看所执行的python所在的路径,以为是bzip2版本不对,就匆忙去下载bzip2的源码进行编译和安装,结果装完发现还是报上面的错,郁闷一阵后再仔细看一下恍然大悟,原来我的环境里安装了多个python,执行脚本里使用的python是使用的手工安装在/usr/local/bin/python而不是默认的/usr/bin/python,这样的话,创建个链接把那个so挂到/usr/local/开头的路径下去就行了:
ln -s /usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so /usr/local/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
另外,注意在arm64环境下因为平台不同名字稍有差异,对应的so是 /usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-aarch64-linux-gnu.so