T-Th 9:05
or
T-Th 11:15
in Olin 155

CS 1110: Introduction to Computing Using Python

Spring 2014

Python in CS 1110

Python comes in several shapes and sizes. To make sure that everything works properly, we require that every single person in the course use the same version of Python [Exception: Mac users running OS X Mavericks (10.9) must use the work-arounds specified below, because of a version mismatch in ActiveState Python.] You are free to either work in the ACCEL labs or to use your laptop; but whatever the case, your version of Python must be the same as what we are using in the labs. Hence, even if you already have Python installed on your computer, we ask that you install our version (see instructions below) so you can be consistent with everyone else.

As you will see, these instructions are a bit long. Installing Python is often much more complicated than simply using it. As many people in this class have not programmed before, we have tried to make installation as simple as possible via step-by-step, "do this, do that" instructions. This is not what the rest of the course is like. You are not expected to understand what many of these steps are doing. Furthermore, these instructions are lengthy mainly because we are trying to support as many operating systems as possible. In practice, you should be able to finish the installation in 15-20 minutes.

If you have problems, and if you have a laptop, the best thing to do is to bring it in to someone to look at. Any of the course staff or consultants can help you with this. If you cannot bring in your computer, we have provided you with some instructions below. If you are having difficulty with these instructions, please post your question to Piazza (if it isn't answered there already), making sure to cut-and-paste in all error messages you received during the installation process.


Table of Contents


Installing Python

While installing the base Python package is usually straight-forward, getting add-ons for Python can be a bit tricky. For that reason, we have tried to simplify this process into as few steps as possible. First you need to install Python. We have decided to standardize on the ActiveState Community Edition of Python. This is a free version of Python that is fairly complete and has a lot of nice features. On Mac OS 10.9, the Python that comes packaged with your system is sufficient, so we will have you just use that one. You will notice that the ActiveState webpage is trying to upsell you to a paid version of Python. Ignore that.

Once you have the base Python install, you need to install the "Cornell Extensions". These are 3rd party extensions packaged by us (but not made by us) to fit into the ActiveState version of Python or the Mac OS 10.9 python. These extra packages include things like graphics capabilities to make games and flashy applications.

Before you install these, be sure to completely read the instructions for your operating system. While you are unlikely to make any mistakes, it is best for you to do things in exactly the order we tell you to. As we said, you do not need to understand what is going on here.


Installing on Windows

The version of Python that we are using for this class is only compatible with Vista, Windows 7, and Windows 8. In the unlikely event that you are using Windows XP, you will not be able to use this version of Python. In that case you will either need to upgrade your OS or work in the ACCEL lab.

This installation process will put Python in the directory C:\Python27. If you already have a folder of that name (which might be the case if you used Python before taking this class), you have two options: move it or delete it. You have to use our version of Python and that is where it wants to go.

Install ActiveState Python

Go to the ActiveState web page and download the free 32 bit Community Edition of Python 2.7.5. This should be the first big blue button on the right. We cannot say the following enough:

Do not download the 64 bit version.

The 32 bit version is called x86, while the 64 bit version is called x64. Even if you are using a 64 bit version of Windows, you must install the 32 bit version of Python. Many of the extra features in the Cornell Extensions will not properly work with 64 bit Python. If you use 64 bit Python, you will not be able to complete some of the later assignments in this class.

Make sure you do not have Python 3.2.2 (or any other number starting with 3). Python 3 is “bleeding-edge” software and too new to safely use in this course (particularly because it is not backwards compatible with Python 2.7).

When you download this installation file, you may get a web page asking you to fill in some information. You can ignore this web page. You do not need to fill in the information; your download has started already.

The file that you download is a MSI installer. Simply double click on it and follow directions. At one point in the installer, it will ask you what directory you want to install in. By default, it will install in C:\Python27; leave this alone and click Next.

Install the Cornell Extensions

Download the file CornellExtensionsWindows.zip. (It's a big file. If you find the download time too slow, you can come to consulting hours to get a copy, or use a USB drive to get a copy from someone who already has it.) Double click on this zipped folder. This will open an Explorer Window with another folder inside of it called CornellExtensionsInstall. Drag the CornellExtensionsInstall folder on to your Desktop. This is important, as we have found that several students have had trouble when installing directly from the zipped folder.

Now that you have a copy of CornellExtensionsInstall on your Desktop, open this folder (again, not the one in the zipped folder!). Inside, you should see the following two items:

Double-click on the file that is named install. This will pop up a window with a lot of scrolling text in it. Do not close the window. When that window closes, Python is successfully installed. If you close the window too early or if the window seems to close pretty quickly on its own, you will have to install a second time.

Once you have finished with the Cornell Extensions, you should test your installation.


Installing on Mac OS X

Please note that these instructions are a bit different depending on which version of the OS you are running. Make sure you know your version and pay attention to where the instructions are different. Snow Leopard (10.6), Lion (10.7), and Mountain Lion (10.8) will use ActiveState Python, like on Windows, but Mavericks (10.9) will use the Python that comes with the OS.

The Python libraries in this class require a 64 bit OS. That means it is only compatible with Snow Leopard (10.6) and later (the current version of OS X is Mavericks, 10.9). If you are still using an older version of OS X (such as Leopard, 10.5), the installation process will fail. In that case, you will either need to upgrade your OS or work in the ACCEL lab.

Installing on a Macintosh is a bit trickier than Windows, primarily because OS X already comes with Python by default. We do not want to use the Apple version of Python on versions of the OS before 10.9, but we do not want to delete it either (deleting System software is always a bad idea). Therefore, you need to be extra careful in to test your installation and make sure you are not using Apple Python on one of these older versions.

Mac OS 10.6, 10.7, or 10.8 only: Install ActiveState Python

Go to the ActiveState web page and download the free Community Edition of Python 2.7.5 (or other number starting with 2.7). You can do this by clicking on the big blue button on the right. Make sure you do not have Python 3.2.2 (or any other number starting with 3). Python 3 is “bleeding-edge” software and too new to safely use in this course (particularly because it is not backwards compatible with Python 2.7).

Just to say it one more time, if you are running Mac OS 10.9, do not install ActiveState Python; it will not work. Instead, follow the Mavericks instructions below.

When you download this file, you will get a web page asking you to fill in some information. Ignore this web page. You do not need to fill in the information; your download has started already.

The file that you download is a DMG. Double click on it and it will turn into a virtual disk drive containing a package installer (an icon that looks like a brown box). Double click on the package installer and follow the directions.

If you are running MacOS 10.8, it may complain that the package is from an unidentified developer. To get around this, control-click on the package and select “Open,” then click “Open” again in the resulting dialog box.

Mac OS 10.9 “Mavericks” only: Install a preliminary set of files

Download the file pygame-1.9.2pre-py2.7-macosx10.7.mpkg.zip. Double click on this file to unzip the contents into a folder somewhere on your computer. When it unzips, you should see a package installer there (an icon that looks like a brown box). Double click on the package installer and follow the directions.

Just to say it again: don't install this package on earlier versions of Mac OS; it is not needed when you are using ActiveState Python.

It is possible that the system will complain that the package is from an unidentified developer. To get around this, control-click on the package and select “Open,” then click “Open” again in the resulting dialog box.

Added March 22: because of a missing file in CornellExtensions_Mavericks-v2.zip, go to http://ethan.tira-thompson.com/Mac_OS_X_Ports.html, click on the link that says "Combo Installer: libpng & libjpg", and run that installer.

All Mac OS versions: Install XQuartz

For any of the OSs, download the file XQuartz-2.7.5.dmg. Double click on this file to unzip the contents into a folder somewhere on your computer. As before, you see a package installer. Double click on the package installer and follow the directions.

All Mac OS versions: Install the Cornell Extensions

If you are running Mac OS 10.6, 10.7, or 10.8, download the file CornellExtensions_PreMavericks-v2.zip.

If you are running Mac OS 10.9, download the file CornellExtensions_Mavericks-v2.zip.

These are big files. If you find the download time too slow, you can come to consulting hours to get a copy, or use a USB drive to get a copy from someone who already has it. But be sure you have the newer installer with “-v2” (version 2) in the filename; don't install the old one. Double click on this file to unzip the contents into a folder somewhere on your computer. As with the earlier steps, you see a package installer. Double click on the package installer and follow the directions.

All Mac OS versions: Run install scripts

For any of the OSs, download the file cs1110_install_scripts.zip, which unzips into a folder containing the files install.sh and fixetc.sh.

The first script sets up your command-line environment so that you will be sure to get the right version of Python, and that Python will know to use the libraries you just installed. You need to run the script install.sh from the command line (the Terminal window). The easy way to do this is to open a new Terminal window, drag the file install.sh and drop it on the terminal window, click to switch back to the terminal window, and hit return. (Dragging a file to the Terminal is a shortcut that does the same thing as typing the full name of the file on the keyboard.) The result should look something like this:

runge:~ srm$ /Users/srm/Downloads/cs1110_install_scripts/install.sh
Searching for .bash_profile...
No .bash_profile found.  Installing...
Installation completed
runge:~ srm$
If you already have customized your command line environment you may see a different message about modifying the same file rather than installing it.

If you tried installing with a previous version of the CornellExtensionsMacintosh or MavericksExtensions packages, you should also run the script fixetc.sh, which undoes some of the work done by the previous installer, because we don't want to leave half-installed fragments around on your machine. Run this script in the same way as the other one. It looks for a problem that may have been left by the previous installer and fixes it if it finds it. If everything was already fine you will see something like this:

runge:~ srm$ /Users/srm/Downloads/cs1110_install_scripts/fixetc.sh
Checking for problems with previous installations...
No problems found.
runge:~ srm$
and if it needs to repair it will ask for your password and look like this:
-bash-3.2$ /Users/testadmin/Downloads/cs1110_install_scripts/fixetc.sh
Checking for problems with previous installations...

IMPORTANT: You must enter your password to fix a (minor) problem.
When you type your password, it will not echo (e.g. it will look
like nothing is happening).  Hit RETURN after you enter your password.


WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type "man sudo" for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:
Problems fixed successfully.
-bash-3.2$
The paragraph labeled “WARNING:” (which may or may not appear) is being printed by a command our script uses internally, and is normal (it needs your password for the same reason many installers do, to allow it to make changes to system files).

Once this is all done, quit the Terminal application so that you won't try to use Python in a window that was opened before the install—that will not work. You are now are ready to test your installation.


Installing on Linux

Linux installation is possible, but it is not as straight-forward as the two main OS options. In particular, to install on Linux, you have to be comfortable with command shell and be ready to compile some of the software from source. If this sounds daunting to you, you might want to rethink about whether you want to use Linux just yet.

In the past, we have had install parties for Linux users. We have worked to make the instructions more self-sufficient this year. However, if there is still demand for a Linux install party, post your request on Piazza .

Install ActiveState Python

The first step is to install Python. Chances are, your Linux machine already has a version of Python. However, one of the key things about this class is that we want to ensure that every Linux distribution is exactly the same. So you need to install the ActiveState version.

Go to the ActiveState web page and download the free Community Edition of Python 2.7.5 (or other number starting with 2.7). This should be the big blue button on the right. Make sure you do not choose Python 3.2.2 (or any other number starting with 3). Python 3 is “bleeding-edge” software and too new to safely use in this course (particularly because it is not backwards compatible with Python 2.7).

Unlike Windows, it is not necessary to download the 32 bit version. You may download the 64 bit version of Python, should you desire. However, your installation must be consistent. That means that every single package that you install must be the same version (if there is a choice).

Install the Basic Packages

The Cornell Extensions package that we provide for Windows and OS X contains a lot of extra Python packages. Instead of one double-click solution, you will need to install each one of these separately.

We find that the easiest way to install is with pip. This is a package manager that comes with Python. To use it, simply open a command shell and type.

    pip install <package-name>

To guarantee success, you generally need to run pip as root. For example, to install Cython, you would type

    sudo pip install cython

In the few instances where pip does not work properly (MatPlotLib has been particularly flaky in the past), you might be able to download a custom installer from the appropriate website.

Now that you know how to install a Python package, here is a list of all the basic packages that you need to install.

Install the Custom Cornell Modules

There are a few extra modules in Cornell Extensions that are unique to Cornell. These modules will be used in the early labs and assignments, so it is important to install them.

The hard part of this step is locating the site-packages directory. This depends on your installation of Python. It is likely to be found in the directory

    /usr/local/lib/python2.7

but we are not sure (and it may not be the same for all distributions). If you do find it, post its location on Piazza so that we can share the information with others.

Once you do find this directory, you need to download the following five files and put them inside it. It is a good idea to change the ownership and permissions of these files to match those of the others in this folder (if you know how to do that).

Install Kivy

The last step is also the trickiest. Kivy is the graphics library that we will be using for several of the assignments in this class. In the past, students have had to compile this library from source code.

Fortunately, the website now appears to have friendly instructions for installing on Linux. We have never tried this instructions ourselves, however, so you are on your own here. Again, if you would like an Linux install party, post your request on Piazza.

Once Kivy is installed, you are are ready to test your installation.


Test Your Installation: Getting Started with Python

Now that you have installed Python, it is a good idea to test it out. While you do not need to know anything about Python yet, you do need to learn how to run Python programs that are already made for you.


The Command Shell

The first thing that you need to do is to familiarize yourself with command shell for your OS. The command shell is a text-based version of the Windows Explorer or Finder; you can use it to move and rename files and folders. It is also how we will run Python in this class.

Using the command shell requires a bit of practice, so we have created a separate page of instructions for this. Right now, all you need to know is how to start up the command shell; you do not need to know any special commands. Start the command shell for your OS and type

   python

This will put you in the "Python Interactive Mode".

The Python Interactive Mode looks exactly like the command shell except that it takes Python commands (and does not respond to operating system commands to list files or change the working directory). While the command shells are different for all operating systems, the Python Interactive Mode should look and behave exactly the same way for everyone. On Windows, it will look something like this.

The > > > symbols are the prompt. To get the shell to do something, just type a Python command. For example, type

    >>> 1+1

and hit Return (do not type the > > >; it is already there). See what happens?

To exit the Python Interactive Mode, type quit() and hit Return. (You can also exit on a Mac with Control-D (hold down the control key and then hit the “d” key); and you can also exit on Windows with Control-Z (hold down the control key and then hit the “z” key).)


Testing a Module

It is possible to run Python modules from the command shell as well. A module is a file (ending in suffix .py) containing Python code. We will use them throughout the course. To run a module, you type

    python <filename>

from the command shell. Do not try to type this in the Python Interactive Mode. This can be a bit confusing at first; it is important to keep the following straight:

  • The command shell is a program for accessing files via text based commands. You start Python in this program, but it is not Python.
  • The Python Interactive Mode is a program that runs inside the command shell. It looks a bit like the command shell, but you can tell the difference by the presence of the > > > prompt.

Running a module does not start the Python Interactive Mode. It runs all of the commands in the file, and then quits immediately when done with the file.

To test that your installation is working properly, we have a cute Python module that displays a GUI with the phrase "Hello World!" inside it, as shown below.

To try out this module, download the file the file helloApp.py and put it in some easy to find folder. Open up a new command window (this app will not work correctly in a command window that was opened before you finished installing everything) and navigate to this folder using the drag-and-drop trick for changing directories. Then type the command

    python helloApp.py

If you get the window shown above, congratulations! You have successfully installed all the Python libraries needed for this class.

If the window does not show, first be sure you're working in a newly opened command window. Then, look very carefully at the error messages. If it says it cannot find the file helloApp.py, then you did not use the drag-and-drop trick correctly; we recommend that you practice with the command shell tutorial some more. Otherwise, there is a problem with your installation of Python. Post your error message to Piazza and we will try to help you out.


Troubleshooting pre-Mavericks OS X

If your installation is not working and you are using OS X 10.6, 10.7, or 10.8, there is one more thing you should try before posting to Piazza. Remember that you now have two versions of Python and we need to make sure that you are running the correct one.

The "application path" is how the Terminal finds the correct version of Python to use. Launch the Terminal and enter the command

   which python

This command asks the Terminal "which version of Python are you using?" If you get the answer

   /usr/local/bin/python

then you have the correct version of Python. In that case, your installation problem must be somewhere else.

However, if you get the answer

   /usr/bin/python

this is the Apple Python, which is a problem (if you are not on OS X Mavericks). If you are absolutely certain that you followed all of the steps above, it is time to see a consultant, as this is not a problem that is easy to fix over Piazza.


Installing Komodo Edit

The last program to install is Komodo Edit. Unlike Python, you will not use this program right away; we will not start to use it until the third week of class. The purpose of Komodo Edit is to make these mysterious modules that you tested in the previous step.

Technically, you can write Python modules in any text editor (e.g. a Word processor that only produces text and does not have fonts or fancy formatting). NotePad and WordPad are text editors in Windows, while TextEdit is provided by OS X. The reason we use Komodo Edit is that it is the same across all platforms. That allows you to collaborate with partners who might be working on other operating systems.

To install Komodo Edit, first download the installer from Active State's website (click on the blue "Download Komodo Edit" button after you follow that link). Do not download Komodo IDE, as that is the paid version. Again, you will be redirected to a website asking for some personal information; ignore that and wait for the download to finish.

The file that you download will either be a MSI installer for Windows or a DMG for Macintosh. Double click on it and follow directions.


Configuring Preferences

One of the most controversial aspects of Python is that it treats spaces and tabs very differently. If you accidentally put a tab in your program, it will cause your program will crash. And you will have a hard time finding it because both tabs and spaces look exactly the same.

To solve this problem, you need to change your preferences to help you distinguish between the two. Open Komodo Edit and then select the Preferences menu option (under Edit on Windows; under Komodo on Macintosh). Choose the Editor category and check the box Show whitespace characters, as shown below.

This will allow you to see both tabs and spaces, and to tell the difference between both. Spaces will look like light dots which are centered vertically on each line. Tabs will look like black arrows. These are illustrated in the examples below.

 
Python with Tabs (Bad)   Spaces Only (Good)

Course Material Authors: D. Gries, L. Lee, S. Marschner, & W. White (over the years)