When you use pip to install Python packages, you may encounter an ‘externally-managed-environment’ error.
I encountered this issue when I upgraded my Python version from 3.10 to 3.12.
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
...
This error occurs because your Python environment is “externally managed” by a package manager, which prevents direct use of pip for system-wide installations to avoid conflicts or issues.
2 Ways to Solve Error: Externally Managed Environment
- Use a virtual environment
One way to solve error: externally managed environment is to create a virtual environment folder in your root path. By creating and using a virtual environment, you bypass the system Python entirely, allowing you to install and manage packages freely without impacting the system-wide Python installation
python3 -m venv ~/py_envs
source ~/py_envs/bin/activate
python3 -m pip install xyz
- Force install
The second option is to do a force install. Using --break-system-packages bypasses this protection, allowing you to install the package directly into the system-wide Python environment.
To do so, add --break-system-packages
at the end of pip
, for example:
pip install xyz --break-system-packages
Explore more
Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.
Top comments (0)