Python 用户指南
Bianbu OS 预装了 Python 3,安装第三方 Python 库时,推荐使用正确的包管理工具,否则可能会破坏系统软件包依赖。
在 Bianbu OS中,您可以通过两种方式安装python依赖项:
- 使用apt安装预先配置的系统Python软件包。
- 创建虚拟环境,使用pip包管理工具安装软件包。
使用apt安装Python包
Bianbu OS中建议通过apt安装Python3的软件包,这些包通常是预编译的,因此安装速度更快。apt会管理所有软件包的依赖关系,安装时会包括运行该软件包所需的所有子依赖项。此外,apt确保在你卸载软件包时不会破坏其他软件包。 例如,要安装Python的科学计算库scipy,请运行以下命令:
sudo apt install python3-scipy
要查找使用apt发布的 Python 包,请使用apt search 。在大多数情况下,Python 包使用前缀python3- :例如,python3-numpy对应于Python的numpy包。
使用pip安装Python包
对pip安装的更改
在Bianbu OS 中,用户无法使用pip将库直接安装到系统版本的 Python 中。尝试在系统范围内使用pip安装 Python 包会输出类似于以下内容的错误:
➜ ~ pip install numpy
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.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.12/README.venv for more information.
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.