在 Manjaro 22、Ubuntu 23.04、Fedora 38 等最新的linux发行版中运行pip install时,通常会收到一个错误提示:error: externally-managed-environment,即“外部管理环境”错误,但这不是一个 bug。
$ sudo pip3 install please-cli
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try 'pacman -S
python-xyz', where xyz is the package you are trying to
install.
If you wish to install a non-Arch-packaged Python package,
create a virtual environment using 'python -m venv path/to/venv'.
Then use path/to/venv/bin/python and path/to/venv/bin/pip.
If you wish to install a non-Arch packaged Python application,
it may be easiest to use 'pipx install xyz', which will manage a
virtual environment for you. Make sure you have python-pipx
installed via pacman.
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.
背后的原因
“外部管理环境”错误背后的原因:Manjaro 22、Ubuntu 23.04、Fedora 38 以及其他的最新发行版中,正在使用 Python 包来实现此增强功能。
这个更新是为了避免「操作系统包管理器 (如pacman、yum、apt) 和 pip 等特定于 Python 的包管理工具之间的冲突」。
这些冲突包括 Python 级 API 不兼容和文件所有权冲突。
解决方案
强制删除此警告,回归到熟悉的操作。
记得将下面命令中的 “x” 替换为实际版本。
sudo mv /usr/lib/python3.x/EXTERNALLY-MANAGED /usr/lib/python3.x/EXTERNALLY-MANAGED.bk
例如,你的python是3.11的话就是
sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.bk
和之前一样,现在您可以直接运行 pip(3) install package_name 命令来安装python模块了。