Setting up a virtual environment for Python and TensorFlow on a MacBook. This method worked for my M2 Mac Air with Monterey OS Version 12.5 and Rosetta 2.
What you need:
- Conda (Anaconda & Miniconda both suffice, see this post on which one better suits you)
- Terminal
Step 1
Open Terminal and create a directory for the virtual environment
mkdir tensorflow_venv
cd tensorflow_venv
The directory will be created under /Users/YourName/tensorflow_venv
Step 2
Activate a Conda environment with a Python version compatible with TensorFlow (I was prompted that TensorFlow does not support Python 3.8, so I created a Python 3.7 environment instead)
conda create --prefix ./env python=3.7
conda activate ./env
Step 3
Now that python is installed successfully into this environment, use Conda to install TensorFlow
conda install tensorflow
The download could take a while. If you see something like this, your environment should be good to go.
Downloading and Extracting Packages
......
Preparing transaction: doneVerifying transaction: doneExecuting transaction: doneRetrieving notices: ...working... done
Step 4 (Setting up in PyCharm)
Open a project in which you want to set up TensorFlow. Go to Preferences >> Project: Your Project >> Python Interpreter.
Click on the current Python interpreter and select “Show All”
Step 5
Click on the “+” sign on the top left to add a new Python interpreter. Under “Base Interpreter”, select: /Users/YourName/tensorflow_venv/env/bin/python[some_version] Checkmark “Inherit global site-packages”, and click “Ok”.
And boom! Your Python interpreter should be correctly configured now. Run the following code to check if TensorFlow is installed.
import tensorflow as tf
print(tf.__version__)
if __name__ == '__main__':
pass