因为 Python 环境是由外部管理的,而不是通过 pip 进行管理,所以在安装的时候会报这个错误.
更多详情可以看官方内容
https://realpython.com/python-virtual-environments-a-primer/
pip3 install --timeout=3600 django==3.2.*
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.
暴力方法,强制删除此警告。
“python3.11” 注意替换为你自己的版本
cd /usr
find ./ -name EXTERNALLY-MANAGED
#./lib/python3.11/EXTERNALLY-MANAGED
mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.bak
pip3 install --timeout=3600 django==3.2.*
Collecting django==3.2.*
Downloading Django-3.2.25-py3-none-any.whl (7.9 MB)
━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━ 3.1/7.9 MB 10.6 kB/s eta 0:07:31
优雅方法,使用pipx
您在上面看到的涉及手动工作。Pipx 使其自动化。
它会自动为您安装的每个应用程序创建一个新的虚拟环境。不仅。它还在 中创建指向它的链接.local/bin。这样,安装该软件包的用户就可以从命令行中的任何位置运行它。
apt install pipx
pipx ensurepath
然后用 pipx install package_name
安装
脱库子放屁法,使用venv
在 docker 里再构件一个python虚拟环境
apt install python3-venv
mkdir -p $HOME/.env && python3 -m venv $HOME/.env/project_name
$HOME/.env/project_name/bin/python -m pip install --upgrade pip
$HOME/.env/project_name/bin/python -m pip install django
网友评论