On MacOS 14, pip install throws error: externally-managed-environment

On MacOS 14, pip install throws error: externally-managed-environment.

What’s the best way to resolve? My background and exploration so far.

I’m a recently retired devops engineer. Mostly used bash and DSLs on the job plus dabbled in Python, Ruby, Go. Now wanting to really learn Python. Working through the Harvard CS50p python and then CS50 AI using python classes, plus the book Learning Python, Lutz.

Running a Mac M2 with MacOS 14. Chose to install both vim and python3 with homebrew. (I don’t explicitly remember that but they show up in brew list and I’ve read that python3 doesn’t come preinstalled since at least Ventura.) Homebrew seems to have maybe been a bad decision.

As soon as I tried pip install…

$ python3 -m pip install requests # or any other module
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 non-brew-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-brew packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

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.

I did google about this for over an hour plus read

https://github.com/python/cpython/issues/102134#issuecomment-1445428402

https://discuss.python.org/t/right-or-best-way-to-install-python-in-macos-10-15-catalina/6204

Earlier I thought, maybe brew uninstall python3 and install it another way but…

$ brew uninstall python3
Error: Refusing to uninstall /opt/homebrew/Cellar/python@3.12/3.12.2_1
because it is required by vim, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies python3

$ brew list | grep vim
vim

I haven’t yet tested brew uninstall vim. I don’t know if it will promt me or just uninstall it, or if I’ll get yet more dependencies.

Then I thought maybe I’ll install requests with brew but…

https://formulae.brew.sh/formula/python-requests is deprecated

My question is how best to preceed from here for a stable platform for installing other python modules on MacOS 14. I’m starting to think that venv or pipx is a good idea in any event. Not sure if I should bother trying to brew uninstall vim and python 3.

Thoughts on best combination of solutions?

  1. Try brew uninstall vim and then uninstall python3. Then install them another way? Assumes I don’t get more warnings when brew uninstall vim.

  2. brew install pipx and use pipx?

  3. brew install vitualenv and use that? Assumes that’s the same as venv.

  4. Other?

1 Like

As mentioned in the error message: Create a virtual environment for your own personal use. Globally installing packages is a great way to break your system python environment, making, for example, vim no longer function correctly. Whether you create it via python -m venv (if that is installed) or virtualenv doesn’t really matter.

pipx solves a completely different problem that you currently don’t appear to have. That is only relevant if you want to install an application without developing.

3 Likes

Thanks Cornelius! I found that I do have venv already installed. I’ll pursue that.

Reading further about pipx it looks like " it lets you install cli applications but NOT libraries that you import in your code." So as you said, that wouldn’t be what I want. Ref: Comparison to Other Tools - pipx

Thanks again.

I’m seeing the same issue, using M1 Pro, running OSX 14.4.1
NOTE: also install python3 and pip3 via homebrew

anyone find a solution?

Yes, solutions have been found. Read the error message.

1 Like

Homebrew wants you to create a virtual env so that pip-installed modules don’t break something in your system. Same is true if working with python installed by apt-get in Ubuntu for same reason. The package makers have gone to warning us not to pip install python modules in the system (OS) because it’s so often that something will break because of version dependency clashes.

venv has worked great for me and is simple. Here’s a couple references and exactly now I installed it and use it.

References…

https://docs.python.org/3/library/venv.html
https://www.studytonight.com/post/python-virtual-environment-setup-on-mac-osx-easiest-way

Here’s what I did…

mkdir ~/.venv

python3 -m venv ~/.venv

#	Creates the following in ~/.venv
#		bin/
#		include/
#		lib/
#		pyvenv.cfg
		
#		does not create pip-selfcheck.json 

# to activate the venv
source ~/.venv/bin/activate

# now you can..
python3 -m pip install <module name>

# and it will install the module in the virtual env

# to deactivate the venv
deactivate # or exit the shell

# you should see the folder name for the venv below your prompt when active, like so
pstivers3@mbp ~/repos/learn/pythonlearn
$ 
(.venv) 

# Note, you can chose any folder location and name that you want for the venv. ~/.venv is typical.
# Your project code can be in any directory.
3 Likes