I get: "error: externally-managed-environment" when running pip3 install-

I code in python and this has never happened before it won't let me run pip3, and if I do "--break-system-packages" then it doesn't work in my scripts, and not everything is in HomeBrew and pipx. Please Help.



MacBook Pro 16″

Posted on Apr 17, 2024 2:53 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 18, 2024 3:18 AM

Don't mix homebrew with the Python3 installation (3.12.3) from Python.org.


Regardless of whether you are using Bash or Zsh for the SHELL, your PATH statement should reflect:


export PATH=".:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:${PATH}"


Because homebrew inherently installs the Apple Command Line tools, there will be a crippled Python 3.9.6 installed into /usr/bin/python3 — and without the correct PATH statement to avoid using that version of Python, you will incur headaches. Any pip3 installations using Apple's Python stores those results in ~/Library/Python/3.9/lib/site-packages.


The Python.org installer will link the binaries from the framework into /usr/local/bin. That is where homebrew would also like to place its Python3 dependencies, but will not do so when the Python 3.12.3 binaries are already there per the Python.org installer.


I have a local folder ~/py_projects in which I create Python virtual environments (e.g. env312) for version-specific installations. MrHoffman has demonstrated that invocation for you. To jump into your virtual environment, and by example, how I do it locally from the SHELL prompt:


source ~/py_projects/env312/bin/activate && cd ~/py_projects/env312
pip3 install -U pip
︙
deactivate


8 replies
Question marked as Top-ranking reply

Apr 18, 2024 3:18 AM in response to PuppyArms

Don't mix homebrew with the Python3 installation (3.12.3) from Python.org.


Regardless of whether you are using Bash or Zsh for the SHELL, your PATH statement should reflect:


export PATH=".:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:${PATH}"


Because homebrew inherently installs the Apple Command Line tools, there will be a crippled Python 3.9.6 installed into /usr/bin/python3 — and without the correct PATH statement to avoid using that version of Python, you will incur headaches. Any pip3 installations using Apple's Python stores those results in ~/Library/Python/3.9/lib/site-packages.


The Python.org installer will link the binaries from the framework into /usr/local/bin. That is where homebrew would also like to place its Python3 dependencies, but will not do so when the Python 3.12.3 binaries are already there per the Python.org installer.


I have a local folder ~/py_projects in which I create Python virtual environments (e.g. env312) for version-specific installations. MrHoffman has demonstrated that invocation for you. To jump into your virtual environment, and by example, how I do it locally from the SHELL prompt:


source ~/py_projects/env312/bin/activate && cd ~/py_projects/env312
pip3 install -U pip
︙
deactivate


Apr 17, 2024 5:01 PM in response to PuppyArms

PuppyArms wrote:

I downloaded it from python.org originally, and it still says the same thing


Which Python is in your PATH first? It looks like you’re not defaulting to what was downloaded from Python.org.


Also see PEP-668 for related details.


One approach to avoid modifying what probably shouldn’t be modified is to enable and use a Python virtual environment:

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt

Apr 17, 2024 3:16 PM in response to PuppyArms

There are three common paths to having Python installed. Installing the Apple developer tools and which loads an older and locked-down Python, installing current Python yourself from the canonical distribution, or loading Python via Homebrew, MacPorts, or another package manager.


Here? If you didn’t already try this, load Python from Python.org, configure that, and see if that works better.


Otherwise, you’ll add troubleshooting your Homebrew or MacPorts install to your task list, if you installed Python via a package manager.

Apr 17, 2024 5:35 PM in response to PuppyArms

PuppyArms wrote:

I don't want to use a VM, this is my bash_profile:

<Bash.log>


Bash uses several scripts at login, and the script you posted doesn’t set PATH.


echo $PATH # command used to view the current PATH 


The Python virtual environment is a feature of Python. It is not and does not use a hypervisor; a virtual machine. Various web servers also have virtual environments, as do some other common constructs. Virtual addressing also doesn’t involve a hypervisor, though hypervisors do use virtual addressing features. For the Python background on this diagnostic and this virtual feature, please read the previously-linked PEP.


Apple is encouraging a migration to zsh, as the built-in bash is only getting more stale.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

I get: "error: externally-managed-environment" when running pip3 install-

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.