Python 在 ubuntu下虚拟环境使用pip安装

vstral 最后更新于 26 天前 2 次阅读 预计阅读时间:


当在ubuntu环境下使用pip安装爆出这种类型错误时候,可以通过使用虚拟环境来避免·报错

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.

解决方法:

# 创建虚拟环境
python3 -m venv myenv

# 激活虚拟环境
source myenv/bin/activate

# 在虚拟环境中安装包
pip install 包名

# 使用完成后可以退出虚拟环境
deactivate

此作者没有提供个人介绍
最后更新于 2025-01-10