Ubuntu24.04LTS中使用apt按照pip,然后pip install时显示如下信息:
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.11/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.
文心一言:这个错误信息表明你正在尝试在一个由Ubuntu系统或某些系统级服务管理的Python环境中使用pip来安装包,而这个环境是“外部管理”的,意味着它可能已经被Ubuntu的某些部分或其他系统级服务所依赖。直接在这个环境中使用pip安装或更新包可能会破坏这些系统级服务或应用的运行。
知道了原因,就很好解决问题了。只要创建一个与之隔离的环境即可。
我这里选择创建一个全局的虚拟环境,然后新建一个actpy
别名来激活它。
创建虚拟环境
cd /usr/local/share/
sudo python -m venv global_python_venv
cd /usr/local/bin/
设置快速激活命令
vim ~/.bashrc
追加
alias actpy='. /usr/local/share/global_python_venv/bin/activate'
然后重新source即可
source ~/.bashrc
标签:LTS,venv,managed,Python,Ubuntu24.04,environment,install,pip
From: https://www.cnblogs.com/rech/p/18335556