WTFTW — Updating Python on Mac OS X Yosemite
What The Fact This Week!
If we listen to the experts:
The version of Python that ships with OS X is great for learning but it’s not good for development. The version shipped with OS X may be out of date from the official current Python release, which is considered the stable production version.
First things first: Install homebrew on MAC OSX Yosemite
ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" (source: http://stackoverflow.com/questions/20381128/how-to-install-homebrew-on-mac-osx)
Will prompt for system password (one you use to log in to your MAC), will then run a bunch of stuff, and then when done should give some sort of a success print.
Install pip
Now if you already have a version of Python installed (most likely you do!) you can do this to just get pip:
sudo easy_install pip
How do you make sure if you have Python already you ask?? Simple I say! Just open Terminal, type python and hit enter, not only does this open up a python console but will also print out the version of Python installed.
Now if you don’t have Python, run this:
brew install python
If you do end up using brew, it installs Setuptools and pip for you. Now when you run the pip command it should print a bunch of usage commands, thus proving that pip is available to use!
Now, am still not happy! Why you ask! Going back to the quote with which I started, the default Python version for MAC OS X is 2.7, which is legacy, 3 and 3+ is future! So you now need to upgrade your Python version, and then make sure the upgraded version gets set as the default version.
Warning: Do not attempt to change or get rid of the default 2.7 version. Many programs available in the Ubuntu repos require python 2.x for compatibility reasons, unfortunately the 3.x version can’t be depended on to run 2.x scripts (backward compatibility hell). But there is no reason to purge 2.7 in order to be able to work with 3.3. As lots of software still depends on 2.7; just keep it lingering around.
Upgrade Python
A safe would be to Step (1) Install Python latest edition(3.5 as of this post), and then Step (2) Create an alias to access it!
Step 1. By this point we already know that Mac OS X comes with Python 2.7 pre-installed by Apple, which sadly is a legacy version. To be able to download the latest version, go to the python downloads page, it should auto detect that you are on a MAC machine and will suggest downloads. Download the latest package (most probably a dmg file), install it by double clicking, just as you would for any other .dmg file. It will prompt you to, go through the installation. On the very first installation screen “Introduction” it specifies that your .bash_profile will be updated to point to Python 3.5 (your whatever version you are downloading)as the command python3.
Good to know: Since this is not a default installed software, unlike Apple’s 2.x, the version 3.x that we are installing goes into the library folder.
Once you are done with the installation process you now have two versions of Python, don’t freak out — this is absolutely fine! In fact recommended often times to balance incompatibility issues. So since the installer added the path in .bash_profile, you can access the latest 3.x installed version by typing this in terminal:
python3
Step 1 done!
Set the (newly) installed version as default Python version
Step 2. Now if you are anything like me, you wouldn’t stop here! I want to make the default interpreter to use python command for Python 3 instead of the default Python 2.x version. A simple and safe way to achieve this would be to use an alias. You can add an alias your MAC’s startup script file. Usually this is .bashrc or .bash_profile file in your home directory. I didn’t have any bash_profile file on my new MAC, but thats where all aliases were defined in my old MAC. So I created one first. Start Terminal, and navigate to your home folder:
cd ~/
Now create a new file:
touch .bash_profile
Edit .bash_profile, you can use any editor for this, I usually just use nano for quick one line updates, and add the alias on a new line:
nano .bash_profile
alias python=python3
Save! (Ctrl X and Y on prompt for nano)
To reload the .bash_profile run:
. .bash_profile
Now when you run python command on terminal, it should use the latest 3.x version that you just installed!
Remember, the old 2.x is still present, just not the default anymore.
Tada! You should be all set now. Happy coding!