树莓派4(32位Linux)Python环境numpy导入依赖错误求助
树莓派4(32位Linux)Python依赖问题排查与解决方案
问题背景
在32位Linux系统的树莓派4上运行Python脚本时,出现numpy依赖缺失报错,尝试卸载重装numpy、使用虚拟环境等操作后仍未解决,当前尝试通过miniconda推进,但问题依旧。
核心错误信息
ImportError: Unable to import required dependencies: numpy
Original error was: libcblas.so.3: cannot open shared object file: No such file or directory
已执行的操作及反馈
1. Python版本确认
joy@raspberrypi:/ $ python --version Python 3.9.2 joy@raspberrypi:/ $ python3 --version Python 3.9.2
2. numpy卸载/重装尝试
joy@raspberrypi:/media/joy/DATA $ pip uninstall numpy Found existing installation: numpy 1.19.5 Not uninstalling numpy at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'numpy'. No files were found to uninstall. joy@raspberrypi:/media/joy/DATA $ pip install --upgrade numpy Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (1.19.5) Collecting numpy Using cached https://www.piwheels.org/simple/numpy/numpy-1.23.4-cp39-cp39-linux_armv7l.whl (12.4 MB) Installing collected packages: numpy WARNING: The scripts f2py, f2py3 and f2py3.9 are installed in '/home/joy/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed numpy-1.23.4
3. 脚本运行报错
Traceback (most recent call last): File "/media/joy/DATA/excel_to_mDB.py", line 3, in <module> import pandas as pd File "/home/joy/.local/lib/python3.9/site-packages/pandas/__init__.py", line 16, in <module> raise ImportError( ImportError: Unable to import required dependencies: numpy: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. Please note and check the following: * The Python version is: Python3.9 from "/usr/bin/python" * The NumPy version is: "1.23.4" Original error was: libcblas.so.3: cannot open shared object file: No such file or directory
4. 虚拟环境操作问题
(venv) joy@raspberrypi:/media/joy/DATA $ python excel_to_mDB.py Traceback (most recent call last): File "/media/joy/DATA/excel_to_mDB.py", line 3, in <module> import pandas as pd ModuleNotFoundError: No module named 'pandas' (venv) joy@raspberrypi:/media/joy/DATA $ sudo pip install pandas Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: pandas in /usr/lib/python3/dist-packages (1.1.5) (venv) joy@raspberrypi:/media/joy/DATA $ sudo pip uninstall pandas Found existing installation: pandas 1.1.5 Not uninstalling pandas at /usr/lib/python3/dist-packages, outside environment /usr Can't uninstall 'pandas'. No files were found to uninstall.
运行的脚本代码
import pandas as pd from pymongo import MongoClient aaa = pd.read_excel("T1_new.xls") print(aaa.head) client = MongoClient('127.0.0.1:27017') db = client['sample_data_in_DB'] collection = db['sample_collection'] collection.insert_many(aaa.to_dict('records'))
解决方案
1. 安装缺失的系统底层库
报错根源是系统缺少libcblas.so.3,直接通过apt安装适配树莓派的线性代数基础库:
sudo apt update && sudo apt install libcblas3 libatlas-base-dev
2. 清理numpy版本冲突
当前系统存在两个numpy版本,导致路径混乱,按以下步骤修复:
- 移除用户本地安装的numpy:
pip uninstall numpy -y
- 用系统包管理器重新安装适配版本:
sudo apt install python3-numpy
3. 正确使用虚拟环境
之前误用sudo pip导致包安装到系统而非虚拟环境,正确操作流程:
- 激活虚拟环境:
source venv/bin/activate
- 直接用
pip安装所有依赖(无需sudo):
pip install pandas pymongo openpyxl
注:openpyxl是pandas读取xls/xlsx文件必需的依赖
4. 放弃miniconda(可选)
树莓派32位系统的miniconda支持有限,容易出现架构不兼容问题,优先使用系统包管理器+piwheels源,piwheels已预编译ARM适配的Python包,兼容性更强。
内容的提问来源于stack exchange,提问作者j ton
相关产品推荐
相关产品推荐

