2025-02-23 11:13AM
pip3 安装python包,报错说:error: externally-managed-environment
$ pip3 --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
$ pip3 install -r requirements.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.
这个是因为:系统不允许在“外部管理环境”中直接安装python包,可以使用虚拟环境来安装依赖
解决方法:
1. 安装 python3-venv,这个是虚拟环境工具:
$ sudo apt install python3-venv
2. 创建虚拟环境:
在你的项目中创建一个虚拟环境,例如:/opt/app/social_data/ 目录中创建一个名为 venv 的虚拟环境
$ python3 -m venv venv
3. 激活虚拟环境:
$ source venv/bin/activate
激活后,你的命令行提示符就有所变化:
(venv) root@root:/opt/app/social_data#
4. 在激活的虚拟环境中,继续安装 requirements.txt
中的包:
$ pip install -r requirements.txt
Collecting elasticsearch==8.12.1 (from -r requirements.txt (line 1))
Downloading elasticsearch-8.12.1-py3-none-any.whl.metadata (5.3 kB)
Collecting pandas (from -r requirements.txt (line 2))
Downloading pandas-2.2.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (89 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 89.9/89.9 kB 794.8 kB/s eta 0:00:00
Collecting openpyxl (from -r requirements.txt (line 3))
Downloading openpyxl-3.1.5-py2.py3-none-any.whl.metadata (2.5 kB)
Collecting pyyaml (from -r requirements.txt (line 4))
Downloading PyYAML-6.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (2.1 kB)
Collecting argparse (from -r requirements.txt (line 5))
Downloading argparse-1.4.0-py2.py3-none-any.whl.metadata (2.8 kB)
Collecting pymysql (from -r requirements.txt (line 6))
Downloading PyMySQL-1.1.1-py3-none-any.whl.metadata (4.4 kB)
Collecting elastic-transport<9,>=8 (from elasticsearch==8.12.1->-r requirements.txt (line 1))
Downloading elastic_transport-8.17.0-py3-none-any.whl.metadata (3.6 kB)
Collecting numpy>=1.26.0 (from pandas->-r requirements.txt (line 2))
.........
5. 完成后退出虚拟环境:
$ deactivate
登录
请登录后再发表评论。
评论列表:
目前还没有人发表评论