In my spare time this summer, I’ve been working through CS101: Building a Search Engine on udacity. I’ve written critical things about the new wave of massive online courses before, in part because I’ve never finished one. That’s not exactly a fair position from which to criticize, even if it’s a reality shared by the vast majority of people who sign up for these things. And, given this week’s announcement from Coursera that a dozen universities have signed on to offer courses through their platform, a little more critical participation may be in order to see the order of things to come. Or at least the order for the 5% who finish these classes.
So, I’ve been working through CS101, answering a few questions in the forum, and getting a feel for the community that exists around the class. One question that has come up repeatedly in the forums is how to access the Python Interpreter to work on solutions for problems before entering them into the course’s interactive programming environment. (BTW, that environment is pretty nifty, and appears to run on a virtual machine that is spun up and persist per session.) There seems to be a fair amount of confusion at times one how to access Python on student’s home machine. Surprising, the python reference included on the course site never covers this. And, the instructor (Dave Evans from U.Va.) frequently references trying things out in the interpreter. I find it a bit curious that the course materials essentially assume students will do all their work on the course site, which means that code snippets don’t get saved and built upon on the student’s own machine. (It’s very easy to take the class in this way, but I think weakens it.) Of course, CS101 isn’t a programming class, but an introduction to computer science principles. That means that most students likely have no programming background, which makes the absence of a tutorial on setting up a local programming environment all-the-more unusual.
In the interest of giving back to a free course, sharing both with those taking the course, and with other first-time programmers, I figure I might as well provide a tutorial of a few options myself. I’m not a professional programmer, but picked up these bits and pieces along the way when I first started to learn some Python.
For CS101 (and similar) students, you have three main options for interacting with the Python Interpreter:
- In interactive mode from the command line.
- With an Integrated Development Environment (IDE).
- With a text editor.
A side note on which version of Python to use. The programming environment for the class uses Python 2.6.6. You can check it for yourself the next time you’re answering a question on the site. Just add this code and hit run:
import os print os.system("python --version")
Given that the course is taught using a 2.x version, it’s highly suggested to install that on your local machine as well. Python 3.x makes a number of backwards incompatible changes to the language. This in one instance where the “latest” isn’t actually preferable.
Some operating systems have python already installed. Some don’t. Before one can work with/in python on their local machine, we have to make sure it’s there!
Easiest to hardest:
Python comes pre-installed on all the different linux distributions. If you use linux, I’m guessing you already know that! Nonetheless, to prove it to yourself. Simply open your Terminal application and enter python at the prompt:
Assuming you’re distribution isn’t too old, your version of python is likely fine for the CS101 course. If not, use your distro’s package manager to upgrade python.
Guess what! Python comes pre-installed on Mac OS X too! Open up Terminal.app and do the same as described above.
You’ll get the same. Depending on which version of OS X you’re using, you’ll likely have either 2.5.1 (Leopard), 2.6.1 (Snow Leopard) or 2.6.7 (Lion). For CS101, any three of those is fine.
If you’d like to use a different version of python DO NOT trash the default installation. That will cause real problems. Instead, follow the instructions here to install python with Homebrew.
Microsoft does not deliver its Windows product with python pre-installed. The easiest option for a Windows user is to take advantage of python.org’s binary installer. Simply go to the download page and chose the appropriate installer. This installer will provide access to python, including the at the command prompt, from your start menu. The installer will create a
C:\Python27 directory. You can also simply open a command prompt and type:
That will put you in the interpreter.
So, now to use the interpreter…
1. Interactive Mode
Python can be used in two different ways. Normally, one would write a script in an IDE or a text editor, an then have the interpreter run the script. In interactive mode, the interpreter evaluates each line of code as you type it in, giving immediate feedback. This is done on the command line (or prompt) as you’ve seen above.
When started, interactive mode will look something like this:
Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >;>;>;
The prompt will take your input, immediately evaluate it, and return the result (if any) to the terminal:
>;>;>; print "I'm enrolled in CS101." I'm enrolled in CS101
If you make a mistake, you’ll get back immediate feedback as well:
>;>;>; print 3+' Three' Traceback (most recent call last): File "<;stdin>;", line 1, in <;module>; TypeError: unsupported operand type(s) for +: 'int' and 'str'
Here, the error is trying to concatenate an integer and a string.
There are advantages and disadvantages to using interactive mode.
- Get immediate feedback as you type a script in, including errors.
- No need to write files to disk, and then run them. From the terminal this means not having to call the specific file like this:
$ python path/to/your/script.py
- Quick and easy to try something simple.
- No syntax highlighting. You don’t get color to mark parts of your code.
- If you make a mistake with, for example, indentation while writing a function, you’ll have to start over.
- There’s no persistence. Once the code has run, you’ll have to type it in all over again to retry or tweak it.
- It’s not suitable for longer scripts.
For CS101, interactive mode can be a useful way to try out solutions to simple bits of code, especially in answering Quiz questions. But, for the problem sets, the limitations noted become much more important. So, how to get persistence?
2. Integrated Development Environments
An Integrated Development Environment (IDE) is a software program that, well, integrates all the tools you would need for software development. IDEs range from very simple to highly bloated with support for many different languages, code completion, debugging, source control, and more. Most IDEs are probably overkill for CS101 students.
For an experience most like the examples demonstrated by Dave in the course lectures, and the option to work in either interactive or normal mode, give a IDLE a try. IDLE is a very simple IDE bundled with python distributions. It offers both an interactive shell, and the ability to write scripts as files and run them from within IDLE. Moreover, in both interactive mode and normal mode, IDLE gives syntax highlighting as seen in CS101. This is very helpful, especially for making sure you close your strings in quotes! Also, in interactive mode IDLE will highlight the spot where syntax errors occur.
Errors show up in red, like a professor marking something wrong.
To write to a file, simply open a new window and start typing. To run that file, on a Mac at least, simply press
F5. Output of the script will show up in IDLE’s interactive shell. Files can be saved and opened in the way you’re used to with other programs on your system. For CS101, this means you can maintain a file with all the functions that form the search engine. Or, you can maintain separate files for individual Problem Set assignments.
To open IDLE on a Mac, type idle in the terminal:
On Windows, you can chose IDLE from the start menu, as part of the python bundle.
Many distros of Linux don’t include IDLE, but it can be installed by your package manager as either
python-tools (Fedora) or
I would argue that full-scale IDEs are overkill for CS101, especially as they usually involve build tools for languages that need to be compiled before execution (which Python does for you). Some cross-platform IDEs include Eclipse (targeted mostly at Java and C/C++ development), Apple’s Xcode (targeted mostly at Objective-C development for OSX and iOS), and NetBeans (also rooted in the Java world). Here’s a much more complete list by language.
3. Text Editors
In my opinion, text editors offer the best option for those new to programming, and especially to people like me who come from an academic humanities background. Some text editors have very high learning curves, like Vim or Emacs. Others are simple to use and quickly powerful. Text Editors tend to be very personal decisions as well, resulting in the kind of fanatical devotion that personal decisions elicit.
The workflow on a powerful text editor is still quite simple. Open a new file, enter your code, and run it from within the editor along the way, either sent to the terminal or within the editor. This may sound a lot like IDLE, but a good text editor is much more powerful. You get code folding, tab completion, line numbering, automatic indentation, some level of debugging, syntax highlighting, and some level of code templating. And not only for python, but for a number of languages. All that, without the complexity or bloat of a full-scale IDE.
I use Textmate, a program I’ve come back to many times after trying to leave it. (This post is written in Textmate with markdown.) When I start a new Unit in the class, I open two windows in Textmate — one for notetaking, and one for code. While following along with the lectures, I take notes in the one file, and start building the code snippets in the other. To try a piece of code written in Textmate, I have two options–>;
CMD-R runs the code and sends the output to a Textmate pop-up window while
SHIFT-CMD-R will open a Terminal window, and run the script there. There are bundles for all kinds of programming and markup languages for Textmate, each of which makes writing in the editor easier and more efficient. These instructions are particular to Textmate, and each editor will have it’s own quirks.
There are many text editor options, both free and for purchase, available for all three platforms. Some to consider:
- TextWrangler — free in the App Store
- Komodo Edit — free, and the little brother/sister of the Komodo IDE
- NotePad++ – free
- E-TextEditor — aiming for the Textmate experience on Windows
- SCiTe — free
These are only a few of many options.
Once you have Python installed, and have experimented with the Interactive Terminal, IDLE, and maybe a Text Editor, you should have a sandbox to play in on your own machine while working through the course. I hope that you’ll find making progress through CS101 a bit easier.
One final note, I’m frequently surprised at the number of questions asked in the forum whose solutions are very simple to find with a google or duck-duck-go or bing search. It should be taken as a truism that if you are interested in learning in the MOOC setting of a udacity or coursera course, you must be willing to hone your google foo. For MOOCs to be successful at all, the participants must approach them with a hefty sense of D.I.Y., though hopefully mixed together with a sense of community — a community of D.I.Y.’ers