SuMio
365 words
2 minutes
通过brew安装的python无法使用pip安装第三方库

在使用brew安装python后,直接使用pip安装第三方插件会遇到如下报错:

  25pm-sumio.github.io git:(main) pip3 install setuptools
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.

这里使用环境管理器创建用于项目隔离的虚拟环境以解决该问题;

#找一个喜欢的地方创建虚拟环境目录,并进入该环境
  python@3.12 git:(stable) python3 -m venv ./env
  python@3.12 git:(stable) source ./env/bin/activate
(env)   python@3.12 git:(stable)
#若要使用brew安装的python库(例如pwntools),请参考https://25pm-sumio.github.io/posts/2024/08/18/01/#5%E4%B8%BApython%E6%B7%BB%E5%8A%A0%E6%89%98%E5%B1%95%E7%9B%AE%E5%BD%95pth
#在虚拟环境的./lib/python3.12/site-packages/mypath.pth中添加pwntools的绝对路径即可

Pycharm使用该虚拟环境编写脚本的配置

0101
通过brew安装的python无法使用pip安装第三方库
https://25pm-sumio.github.io/posts/2024/09/12/01/
Author
簌澪SuMio
Published at
2024-09-12