如何在 pip 中自动设置 --break-system-packages
问题
当你使用 pip 安装 Python 包时,在较新版本的 Debian 或 Ubuntu 上,你将看到以下警告:
setup.txt
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.解决方案
你可以用正确的方式(使用 pipx 或类似工具),但既然你在阅读本文,我假设你只是想消除警告而不必每次调用 pip 时指定 --break-system-packages。
为此,以你运行 pip 的用户身份运行以下 shell 脚本:
set_pip_break_system_packages.sh
mkdir -p ~/.config/pip
echo "[global]" >> ~/.config/pip/pip.conf
echo "break-system-packages = true" >> ~/.config/pip/pip.conf之后,pip 每次运行时都会自动使用 --break-system-packages。也就是说,它会按你预期的方式运行。
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow