Python 报错 error: externally-managed-environment 的解决方法
我们先看报错:
~/input_method » pip3 install requests 1 ↵ voosk@Macmini
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 近几年(PEP 668)为了保护系统安全引入的新规定。它怕你直接用 pip 把系统自带的 Python 环境搞乱了,导致 macOS 一些底层功能崩溃。
我用 requesrs 这个库来举例子。
有两种方法可以解决这个问题:
- 直接在安装命令后面加个参数,告诉系统:“我知道我在干什么,出了事我自己负责”。
命令行执行pip3 install requests --break-system-packages。
这样就能直接装上。虽然名字叫break-system-packages听起来很吓人,但对于只装一个requests库来说,大概率不会出问题。 - 还有一个更专业、符合规范的方法,那就是使用“虚拟环境”。
执行python3 -m venv venv命令创建虚拟环境。
执行source venv/bin/activate命令激活环境。
此时有些终端提示符前面会多了个(venv)字样。
使用第二种方法,创建虚拟环境以后再执行 pip install requests 即可正常安装。
~/input_method » pip install requests voosk@Macmini Collecting requests Using cached requests-2.32.5-py3-none-any.whl.metadata (4.9 kB) Collecting charset_normalizer<4,>=2 (from requests) Downloading charset_normalizer-3.4.5-cp314-cp314-macosx_10_15_universal2.whl.metadata (39 kB) Collecting idna<4,>=2.5 (from requests) Using cached idna-3.11-py3-none-any.whl.metadata (8.4 kB) Collecting urllib3<3,>=1.21.1 (from requests) Downloading urllib3-2.6.3-py3-none-any.whl.metadata (6.9 kB) Collecting certifi>=2017.4.17 (from requests) Downloading certifi-2026.2.25-py3-none-any.whl.metadata (2.5 kB) Using cached requests-2.32.5-py3-none-any.whl (64 kB) Downloading charset_normalizer-3.4.5-cp314-cp314-macosx_10_15_universal2.whl (280 kB) Using cached idna-3.11-py3-none-any.whl (71 kB) Downloading urllib3-2.6.3-py3-none-any.whl (131 kB) Downloading certifi-2026.2.25-py3-none-any.whl (153 kB) Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests Successfully installed certifi-2026.2.25 charset_normalizer-3.4.5 idna-3.11 requests-2.32.5 urllib3-2.6.3 [notice] A new release of pip is available: 26.0 -> 26.0.1 [notice] To update, run: pip install --upgrade pip