树莓派安装python第三方库如keras,tensorflow
1. 在安装python 相关包时出现如下错误提示:
cherry@raspberrypi:~ $ pip3 install keras error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. For more information visit http://rptl.io/venv 编辑 Raspberry Pi OS - Raspberry Pi Documentation note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification.
2. 原因:
这是 Bookworm 及以后版本 的 Raspberry Pi OS 对系统 Python 的强制保护机制(PEP 668)。
官方只允许用 apt
装系统包,或用 venv 隔离第三方包。
3. 解决方案:
1)创建并激活虚拟环境
python3 -m venv ~/venv
source ~/venv/bin/activate
2)安装你需要的任何包
pip install keras tensorflow
注:如何每次开机自动进入 venv
把 source ~/venv/bin/activate
加进 ~/.bashrc