Reinstalling the Python machine learning environment in my Mac

That is conda and python 3.7, tensorflow, keras, numpy, pandas, jupyter, sklearn etc.

Photo by Wesson Wang on Unsplash

Something went wrong with my previous installation of Anaconda on an upgrade and I decided to reinstall it. Debugging it, would have probably taken more time (if possible at all), while a fresh start is always welcome as long as your code and data are secure. And the Anaconda installation is totally and easily replaceable. So straight to the steps:

Step 1: I removed my old installation

  • The Anaconda folder was in ~/anaconda so I did a rm -rf anaconda3 while in my home folder.

Step 2: I installed Anaconda again

  • Downloaded and run the installer for Python 3.7
  • Followed the standard, as is installation. This would also install Anaconda paths on my zsh profile. And indeed it did (tail -n 20 ~/.zshrc):
  • Anaconda installed at ~/opt/anaconda3

Step 3: I verified the installation

  • From the Applications folder I opened the Anaconda Navigator (didn’t wait until it was indexed by Spotlight).

Step 4: I also opened a terminal and run a few commands

  • conda -V
  • everything seems to be working

Step 5: I updated conda to latest version (and not all the packages)

  • conda update conda (this took conda from 4.8.2 => 4.8.3)

Step 6: I ensured that the basic libraries I want are installed from a script in this Machine Learning Mastery blog post:

# scipy
import scipy
print('scipy: %s' % scipy.__version__)
# numpy
import numpy
print('numpy: %s' % numpy.__version__)
# matplotlib
import matplotlib
print('matplotlib: %s' % matplotlib.__version__)
# pandas
import pandas
print('pandas: %s' % pandas.__version__)
# statsmodels
import statsmodels
print('statsmodels: %s' % statsmodels.__version__)
# scikit-learn
import sklearn
print('sklearn: %s' % sklearn.__version__)
# tensorflow
import tensorflow
print('tensorflow: %s' % tensorflow.__version__)
# keras
import tensorflow.keras
print('keras: %s' % tensorflow.keras.__version__)

I have created a gist for that so:

The standard machine learning libraries I wanted are installed in the base environment so what’s left is to also add TensorFlow to complete the installation. But before that I’ll check also that Jupyter notebooks are running.

Step 6: Check Jupyter notebooks

From the command line with jupyter notebook I saw the Jupyter environment in my browser (http://localhost:8888/tree) and I was able to also create a new notebook in Python 3.

Step 7: Clone the base environment and create a new one for tensorflow

So now the idea is to create a new environment for TensorFlow, but in addition keep the base packages. To do that I cloned the base env:

conda create -n tf — clone base

Step 8: Install TensorFlow (and Keras)

After activating the tf environment with conda activate tf the next step was to install TensorFlow in that environment. I did that with conda install tensorflow.

One can see the mkl prefix in the tf packages.

I installed it from conda instead of tensorflow due to the extra speed-ups provided by the the conda TensorFlow packages are built using the Intel® MKL-DNN library,

As for Keras, it is included in TensorFlow and as recommend by the keras team:

At this time, we recommend that Keras users who use multi-backend Keras with the TensorFlow backend switch to tf.keras in TensorFlow 2.0.

Step 9: Testing

To ensure everything is working as planned (1) I re-run the script:


And (2) I created a notebook and “copied-pasted” this example changing:

from keras… to from tensorflow.keras…

The example run smoothly:


Since the Mac I have is old with no GPU (upgraded MacBook Pro late 2012) I stopped and this point.

Mac O’Clock

The best stories for Apple owners and enthusiasts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store