To install Keras on a Mac, install TensorFlow (which includes Keras) using
pip install tensorflow
or
pip install tensorflow-macos for Apple Silicon (M1/M2/M3) chips.
For best performance on M1/M2/M3 chips, use conda to install tensorflow-deps.
Verify with python -c "import keras; print(keras.__version__)"
== Use PIP (Standard & Apple Silicon)
# Upgrade pip
pip3 install --upgrade pip
# Install TensorFlow (includes Keras)
pip3 install tensorflow Note: TensorFlow works with Python 3.9-2.12. The recommended way to install and manage multiple Python versions on a Mac is by using a version manager like pyenv, which prevents conflicts with the system's default Python installation.
== Use pyenv (Recommended for Developers)
brew install pyenv
You may also want the pyenv-virtualenv plugin for creating isolated project environments:
bash brew install pyenv-virtualenv
Configure your shell: You need to initialize pyenv in your shell (e.g., Zsh, the default for modern macOS). Add the necessary lines to your shell's configuration file (commonly ~/.zshrc or ~/.bashrc) and restart your terminal as per the pyenv documentation instructions.
pyenv install 3.12.0
Switch between versions:
- Globally: Set a global default version for all your shell sessions:bash
pyenv global 3.12.0 - Per Project: Navigate to a project directory and set the local Python version, which creates a
.python-versionfile:bashpyenv local 3.12.0 - Create a Virtual Environment: Use
pyenv-virtualenvto create an isolated environment for a project with a specific Python version:bashpyenv virtualenv 3.12.0 myprojectenv cd myprojectdir pyenv local myprojectenv
Create local env:
# Create a virtual environment (e.g., named 'tf-env')
python -m venv tf-env
# Activate it (Linux/macOS)
source tf-env/bin/activate Then install:pip install tensorflow Verify:import tensorflow as tfprint(tf.keras.__version__)print(tf.__version__)
No comments:
Post a Comment