On my MacBook Air M1 under Monterey 12.3 and the brew-installed pythons python@3.8 and python@3.9 in their standard locations at /opt/homebrew, together with Xcode 13.3 at its standard location, after looking for hours I finally noticed the following and have this theory:
First of all I don't like to use conda or miniconda etc. (only non-python dependency I seemed to lack was hdf5 anyway). Also, I confirm that creating a venv mentioned above by tensorflow support works just fine. But if I just want to pip install tensorflow-macos and tensorflow-metal in the system locations for brewed pythons:
brew install hdf5
HDF5_DIR=/opt/homeware python3 -m pip install tensorflow-macos
If at the last stage the plugin tensorflow-metal gets installed with:
python3 -m pip install tensorflow-metal
while using a brew-ed python@3.9 from /opt/homebrew/bin/python3 at the brew system default dir:
/opt/homebrew/lib/python3.9/site-packages/tensorflow-plugins
then I get the error about the METAL plugin having been already registered.
If however I install in my user directory library instead ~/Library/Python/3.9/lib/python/site-packages via
python3 -m pip install --user tensorflow-metal
then everything works. Note this concerns just the plugin, i.e. the tensorflow-macos package can still be at the brew system location.
I noticed all this, because from past Xcode Python (3.8) usage I had a user packages directory (Python Library) at ~/Library/Python/3.8/lib/Python/site-packages (only place you can add packages when using the system Python) and since it existed, the brewed python@3.8 installed everything there without me supplying --user to pip.
So I banged my head against the wall as to why brewed python@3.8 works and python@3.9 doesn't. I cleaned out all my packages from python@3.9 system directory via pip uninstall, even purged the entire system site-packages directory and still with a clean setup using the two simple pip steps it kept failing. That's because there was no user packages directory for 3.9 and everything kept going back to the brew system directory.
I am quite convinced that is not a case of some configuration or package mix clashing between various pythons as I have been very careful to clean everything before I reproduce. Furthermore, moving the installation of tensorflow-metal under python@3.8 from my user package directory to the brew system directory reproduces the error under python@3.8 that was previously working.
Long story short: There is a bug with tensorflow plugin initialization that somehow triggers when tensorflow-metal plugin is installed under /opt/homebrew causing its registration to be called twice. This is avoided when installed in local --user directory.
I have wasted enough time to make this work and I am happy with my theory for now so will drop further looking into it. I just thought I might shed some light for anyone else down the road in case they come across this.