Mac python3.12 执行pip/pip3异常externally-managed-environment

发布于:2025-06-29 ⋅ 阅读:(15) ⋅ 点赞:(0)

环境:Mac、Python3.12.x版本(3.12.5)

问题:执行pip install xyz 后出现异常:

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.

-------------------------------------------------------

解决方法:
删除对应目录下的 EXTERNALLY-MANAGED 这个文件即可:

which python3 找到python的目录然后进入子目录:3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12

cd /usr/local/Cellar/python@3.12/3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12
rm -r EXTERNALLY-MANAGED

注意:如果担心直接删除导致环境异常,可以先将该文件移走
再次执行 pip3 install xyz 即可 


网站公告

今日签到

点亮在社区的每一天
去签到