Get Python in your Terminal on the Mac

Twice in the last two weeks I helped someone with getting started with Python in the command line on the Mac. You could just download it from python.org and use the built-in IDE, but that’s just not quite powerful enough for many needs. Besides, having some success with the command line is sort of like a badge of honor if you’re a geek, so why not have a fiddle?

The interesting thing about installing python on the Mac is that there are actually two pythons. The most up-to-date one is Python3, which is backwards-incompatible with previous incarnations, and is the recommended one if you’re just getting started. However, regular old Python still rears its head in a lot of different places. Most things just haven’t been updated for Python3 yet, for example Mediacore and the NLTK.

The other interesting thing is that every Mac already does have a system python, which is installed in /usr/local/bin. But we don’t want to use that. For one thing, you don’t want to screw up the system python. You probably won’t screw it up, but just in case. And for another thing, Apple Python is customized, it isn’t documented anywhere how it’s customized, and on top of that it doesn’t include the latest version. So we’ll want to put in a fresh copy of two pythons, then.

Python3

First we’ll started by first installing Python3.

brew install python3

Wait, you do have brew, don’t you? If not, stop what you are doing and get that. Oh, you’ll need to have XCode installed, or, more specifically, the command line tools that come with it. If you’re a dev, though, you already got that, right? Easiest way to check if you’re good is to type at the terminal:

which cc

If you get /usr/bin/cc as a response, you have it. If you get nothing back, you don’t have it. After brew has done its work, check that python3 is installed:

which python3

That will spit back its location, which tells you that it’s installed and working. We’re not done though. What we’re going to do is set up a virtual environment of python3 so that we can install all kinds of crap in there and won’t affect anything else. We want to install virtualenv globally, which means we can use pip or easy_install:

pip install virtualenv    #or 
easy_install virtualenv

This gives us access to a command that makes a new virtual environment. I always put them into the ~/env folder, where each environment is represented by a folder inside there. So let’s make one now.

virtualenv -p python3 ~/env/first

The -p option tells virtualenv which python to use, and since we know from above that python3 leads to our recent install, it’ll use that and make a new environment. Now we have to activate it, which we do by sourcing a special command that’s installed.

source ~/env/first/bin/activate

After that, we have a new command line prompt to remind us that we are now in a virtual environment. If we type

which python

The location that spits back at us is the local virtual environment. Did you notice that I didn’t type python3? It’s just python now.

Python 2

The procedure for regular Python is a bit more involved. After doing this:

brew install python

when we type

which python

We’re supposed to be given the new python that we just installed, right? Oh no, we’re given the Apple-installed version, which is not what we want. This is because brew isn’t about to change things automatically so that everything starts using the new python that was just installed. It’s okay for users like yourself to use them, no problem. Question is, how do we tell the computer to start using it?

We need to adjust the PATH variable so that we, as the user, when we type “python” it seems the brew version. So, where’s the brew version. This command will find it for us:

brew ls python | 
    grep 'python$' | 
    grep -v 'Frameworks' | 
    sed 's/\/python$//'`

What that does is uses brew to output all the files that our installation put in, and then uses a bit of searching and replacing to get the required data. Copy the data that spits out after the command. Now, let’s adjust your PATH variable:

nano ~/.bash_profile

And then in the last line enter this:

PATH=/usr/local/Cellar/python/X.X.X/bin:${PATH}

Note, the X.X.X is going to be whatever version is correct for you, which at the time of writing was 2.7.3. Now, if we source that .bash_profile file, or open a new terminal window, we should see the brew installed version of python available to us upon executing the “which python” command.

Let’s start installing stuff. Create a virtualenv:

virtualenv -p python ~/env/nltk
source ~/env/nltk/bin/activate
pip install nltk
pip install IPython

And now we’ve got a wonderful, nice little place for us to play around in.

2 Comments

  1. Cliff

     /  October 1, 2013

    I get the students to just load up terminal and start the pre-installed version of Python to use the calculator functions if they need to work with large numbers. It’s just convenient, and everywhere.

    For example we do an assessment which involves investigating the unit digits in various powers of a number. e.g. 3^1=3, 3^2=9, 3^3=..7, until we get cycling sequences. They can then use the pattern to predict what 3^2313 would be.

    Using Python in Terminal they can then test their prediction very easily just typing 3**2313. (Calculator, and Excel just don’t show the necessary digits)

    • Adam Morris

       /  October 1, 2013

      I actually did install a non-system version of Python onto the school image, python3 as well.

      Anyway, you’re right, for that use you’re just fine with the system python. Very cool.