如何在 MacOS 系统范围内安装 Python 包?

问题描述 投票:0回答:1

例如,如果我运行:

pip3 install matplotlib

我收到错误:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:
    
    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz
    
    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with
    
    brew install pipx
    
    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.
    
    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.
    
    Read more about this behavior here: <https://peps.python.org/pep-0668/>

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.

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: python3.12 -m pip install --upgrade pip

当我这样做时:

brew install matplotlib

即使在 venv 中安装 matplotlib 只需几分钟,它也会永远挂起,甚至几个小时后。

什么给予?如何在 MacOS 上使用 pip3 在系统范围内安装 matplotlib,并绕过愚蠢的外部管理错误?

python python-3.x pip
1个回答
0
投票

尝试

python3 -m pip install matplotlib --break-system-packages

如果您一直这样做,那么编辑 pip.conf 文件也很有意义。该文件通常位于 macOS.conf 的 ~/.pip/pip (尽管如果它不存在,您可能需要创建它)。将以下行添加到您的 pip.conf 文件中:

[global]
break-system-packages = true
user = true

最后升级pip:

python3 -m pip install --upgrade pip --break-system-packages

这将绕过 PEP 668 保护并允许全局安装软件包而无需创建虚拟环境。

© www.soinside.com 2019 - 2024. All rights reserved.