Tuesday, 23 February 2016

Setting up Scientific Python on a Mac with El Capitan

Since it may be of use to others and so I don't forget it myself, here's how I set up my Mac to run a Python scientific bundle in a virtual environment.  Both of my macs use El Capitan as the OS but this should work for some earlier operating system versions as well.

Step 1 - Chown /usr/local

When El Capitan came out it implemented something Apple calls System Integrity Protection (SIP).  This protects certain system files from being modified or tampered with.  Unfortunately we need to do exactly that, so to work around this you'll need to chown the '/usr/local' directory.   Run the following in terminal:

 sudo chown $(whoami):admin /usr/local && sudo chown -R $(whoami):admin /usr/local  

You'll be prompted for your admin password when you do this.

Step 2  - Xcode and Command Line Tools

Install Xcode and Command Line Tools.  This can be downloaded from the app store for free.  Once this is done you'll need to fire up Xcode to complete the set up.  Once this is done you'll need to get the Command Line tools.  Run the following in terminal:

 xcode-select --install  

Step 3 - Homebrew


Install homebrew by typing the following into terminal:

 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

You might also want to make some changes to your ~/.bashrc file as well.  I added

 export PATH="/usr/local/bin:usr/local/sbin:$PATH"

 

 Step 4 - Get Python

In terminal type:

 brew install python3  

The latest version of python will now install.  It is possible to install specific versions of python using homebrew but I  have never had the need to do it.  You should also add the following to your ~/.bashrc file

 export PATH="/usr/local/share/python:$PATH"

Step 5 - Install freetype, libpng and ffmpeg

Freetype is a software library used to render fonts, libpng is used for portable network graphics (PNG) and ffmpeg is used to record, convert and stream audio and video.  To install them type the following into terminal:

 Brew install libpng 
 Brew install freetype  
 Brew install ffmpeg  

Step 6 - Install gcc and pkg-config

gcc provides a front end for C, C++ and Fortran.  Some python libraries make use of this.  When you install this don't be surprised if it takes a while and terminal appears to hang.  Don't worry it's doing stuff in the background.

 Brew install gcc  
 Brew install pkg-config  

Now we'll create a virtual environment where we can run and create python projects

Step 7 - Install Virtualenv and create a virtual environment

As it's name suggests virtualenv allows us to create virtual environments.  To make it easier to use it's a good idea to install virtualenvwrapper as well. 

 Pip3 install virtualenv  
 Pip3 install virtualenvwrapper  

You might also have to add the following to your ~/.bashrc file:

 export WORKON_HOME=$HOME/.virtualenvs  
 export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3  
 source /usr/local/bin/virtualenvwrapper.sh  

You can now create your first virtual environment by typing the following into terminal:

 mkvirtualenv env1  

I've called the environment env1 but you can call it whatever you like.

Your terminal window should now look similar to this.

All the remaining steps take place in this virtual environment, so don't deactivate it.

Step 8 - Install iPython


The iPython (Jupyter) notebook is one of the most popular ways to create and edit python code.  I use a mixture of this or PyCharm depending on what I am working on at the time.   Type the following into terminal:

 pip install iPython[all]  

Step 9 - Matplotlib, Numpy, Pandas and Scipy


In most scientific python bundles there are four workhorse libraries.  Matplotlib has great data visulaisation tools.  Numpy is used for a lot of numeric and array processing.  Pandas is used for data analysis.  Scipy is used for scientific computing tasks.  To install these type the following into terminal:

 pip install matplotlib  
 pip install numpy  
 pip install pandas  
 pip install scipy  

It is possible that you might have to let matplotlib know which backend you are using.  To do this you will need to edit the matplotlbrc file which is in '~/.virtualenvs/yourenv/lib/yourpythonversion/site-packages/matplotlib/mpl-data/'  You should now have python and a great bunch of libraries running on your mac.  To fire it up first make sure that your virtual environment is active, then type:

 jupyter notebook  

You'll now get a new Jupyter page in your browser.



Of course you may need to add other libraries from time to time and you can do that much in the same way I've shown here for adding libraries to python.

Lastly at some point in the future you may want to update all of your libraries to the most current version.  Rather than do this one library at a time here's a neat bit script to do it.

 pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U  


No comments:

Post a Comment