Charlie
Charlie

Reputation: 33

Installing keras, TensorFlow2 on MacBook Air with Apple M1 Chip

I recently bought a MacBook Air with the Apple M1 chip, and I'm trying to install keras for Python 3.9.10 (installed using homebrew). Using the command

pip3 install keras

in the terminal, I get the following output:

Collecting keras
  Using cached keras-2.8.0-py2.py3-none-any.whl (1.4 MB)
Installing collected packages: keras

Successfully installed keras-2.8.0

Next, I entered the following series of commands (find output included as well):

~ % python3
Python 3.9.10 (main, Jan 15 2022, 11:40:53)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras

After running the last line (import keras), I got the following output in the terminal:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/lib/python3.9/site-packages/keras/__init__.py", line 21, in <module>
    from tensorflow.python import tf2
ModuleNotFoundError: No module named 'tensorflow'

My understanding was that keras is built on top of TensorFlow, however this seems to indicate that I need TensorFlow installed on my computer as well. Is this correct? Or is there something else I'm missing?

I've heard that TensorFlow can be very difficult to install on Apple M1 computers, so I've been trying to avoid doing so, but if I do need to, how should I go about it?

Thank you ahead of time.

UPDATE:

I decided to try and install TensorFlow2 in the terminal using the command

pip3 install tf2

and got the following output:

ERROR: Could not find a version that satisfies the requirement tf2 (from versions: none)
ERROR: No matching distribution found for tf2

I guess this is where the difficulty starts... is there any straightforward way to install TensorFlow2 on an Apple M1 computer? I'd really prefer not to switch compilers or anything as I don't have a great deal of experience with macOS and don't want to make any irreversible mistakes.

Thank you again.

UPDATE 2:

I just tried the command

pip3 install tensorflow

and I got the following output:

ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow

Thank you to Dr. Snoopy for pointing out that the package was called tensorflow and not tf2. I've read the problem might have something to do with a missing multi-threading file or something, but still can't figure out how to fix it.

Upvotes: 0

Views: 11874

Answers (1)

Pe Dro
Pe Dro

Reputation: 3033

A tested method using conda:

  • Ensure you upgrade Mac OS X > 12.3. enter image description here

  • Install XCode CLI tools

xcode-select --install
  • Install Miniforge. I prefer installing using brew
brew install miniforge
  • Create a conda environment and activate it
conda create --name tensyflow python=3.8
conda activate tensyflow
  • Install Tensorflow-MacOS
conda install -c apple tensorflow-deps
pip install tensorflow-macos # or pip3

Upvotes: 5

Related Questions