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.
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:
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:
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.
After that, we have a new command line prompt to remind us that we are now in a virtual environment. If we type
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.
The procedure for regular Python is a bit more involved. After doing this:
brew install python
when we type
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:
And then in the last line enter this:
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.