使用virtualenv在Mac上为Python 2安装TensorFlow遇问题
Hey there! You've got TensorFlow up and running for Python 3, but to get it working in your Python 2 environment, you'll need to create a separate virtual environment dedicated to Python 2—your existing ~/tensorflow env is tied to Python 3, which is why you’re seeing that "already up-to-date" message for the Python 3 version of TensorFlow. Let’s fix this step by step:
Step 1: Create a Python 2-specific virtual environment
First, explicitly specify Python 2 as the interpreter when creating your new virtual environment. Run this in your terminal:
virtualenv --system-site-packages -p python2 ~/tensorflow-py2
- The
-p python2flag tells virtualenv to use your system’s Python 2 interpreter. - We’re using
~/tensorflow-py2as the directory name to keep it clearly separate from your Python 3 environment.
Step 2: Activate the Python 2 virtual environment
Navigate into the new environment and activate it:
cd ~/tensorflow-py2 source bin/activate
You’ll notice your terminal prompt changes to (tensorflow-py2)—this means you’re now working inside the Python 2 virtual environment.
Step 3: Install TensorFlow for Python 2
Important note: TensorFlow stopped supporting Python 2 starting from version 2.0, so you’ll need to install the last compatible release (1.15.0). Run this command:
pip install --upgrade tensorflow==1.15.0
This will install the Python 2-compatible TensorFlow package directly into your new virtual environment.
Step 4: Verify the installation
To confirm everything works as expected, run this quick test:
python2 -c "import tensorflow; print('TensorFlow version:', tensorflow.__version__)"
You should see the version number 1.15.0 printed without any import errors.
Why your original command didn’t work
Your initial ~/tensorflow virtual environment is configured to use Python 3. When you ran pip install --upgrade tensorflow there, it was only checking and updating the Python 3 version of TensorFlow—this has no impact on your Python 2 environment, which is why you got the "already up-to-date" message.
内容的提问来源于stack exchange,提问作者Jane Sully

