Setting Up Your Environment

In this course, we will be using the Python programming language to explore computational thinking. Python is a popular general-purpose programming language that is used in a variety of domains from from rapidly prototyping software, performing statistical analysis and visualizing data, to simply automating repetitive tasks.

In this lab, you will set up your Python development environment which consists of the Python interpreter and its libraries along with a suitable text editor to write programs. If you brought a laptop with you that is running Windows, OS X, or Linux, you can set up the environment on it. Otherwise, you are free to use the Linux lab machines in Moore 207. Irrespective of your choice of where you set up your environment, you will need to interact with the Linux lab machines throughout the course, e.g., to run our test scripts to verify that your programs work. Thus, in addition to setting up your machines, we will also go over how to get things done using the Linux.

Unlike the rest of the labs in this course, you should do this lab individually as it involves setting up your own machine preferences. However, you should do the lab with your partner by your side so that you can ask them questions if you get stuck.

Part 1: Navigating the Linux Terminal

First, read this tutorial on how to use the Linux terminal.

You should read this tutorial while logged into a Linux machine—either the lab machines in Moore or your own laptop if it has Linux on it. As you read, make sure to try out the discussed commands and explore their features.

Part 2: Choose a Text Editor

In addition to navigating and manipulating files through the terminal window, you will also need to edit files—in particular Python source files—through a text editor. A text editor is a lightweight program to view and edit text files. In particular, good text editors provide support for syntax highlighting and navigation of source files in different programming languages. They do not approach the complexity and power of a full-on integrated development environment (IDE) for programming, but are quicker to use for smaller projects—exactly the sort of programs that we will develop in this course.

Because of this, we will heavily encourage you to try out and adopt a text editor for this course. There are a number of text editors available on Linux, but the two most prevalent are:

  1. Emacs
  2. Vim

Our requirements for Python source file text editor are minimal; the only thing we really need is syntax highlighting for Python code which is provided by default in both Emacs and Vim. Both editors are available on every operating system under the sun, so you can have a similar editing experience whether you are on the SEAS machines or your own personal machine.

While Emacs and Vim may feel somewhat archaic to use with somewhat cryptic keyboard shortcuts and commands, your continued practice with them will pay off the long run when you find yourself working in a command prompt. Which text editor you use for this course is ultimately up to you. However, if you are undecided or have no preference, we recommend starting with Emacs.

Once you have chosen an editor, please work through one of these appropriate tutorials on how to use it:

There are also multitudes of other tutorials online—I recommend searching for them and trying them out. My personal favorite is Vim Adventures (disclaimer: I’m a Vim user)

Part 2: Terminal Test—Your Own Webpage

As an exercise in using your new-found terminal skills, try making a basic webpage on your SEAS account. Every SEAS account is equipped with the capability of hosting a personal webpage, and a webpage is only a specially formatted text file!

First, go to the SEAS account management and go to the account management page. From here, click on “Manage your webpage” in the sidebar, and then click the “Configure my SEAS account for web pages” link. This will setup appropriate directories in your Unix account to host a webpage.

Next, log into your SEAS Unix account and go into the public_html directory. Here, create a text file called index.html and copy in the following HTML code:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Webpage Title</title>
</head>

<body>
  The content of your webpage.
</body>
</html>

Feel free to change the title and content accordingly.

Next, change the permissions of index.html so that they are world readable (but not writable). You can achieve this with the 755 permissions string.

Finally, if you have done everything correctly, you can view your webpage at the following URL:

While web programming is not the focus of the course, if you find yourself with some downtime or are just curious, feel free to expand your webpage further or otherwise use your SEAS account to experiment. Here is one tutorial on HTML to get you started: HTML tutorial on htmldog.com.

Part 3: Setting up Your Development Environment

Now, let’s get Python set up on the machine that you will work on in this course. If you choose to work on the Linux machines, you are already done! Everything has been set up for you.

However, if you would like to work on your own laptop, you will need to install:

  1. Python
  2. Pygame

As well your chosen text editor. Download and install the latest Python 2 drop, 2.7.12, first. And then you can install the relevant Pygame drop from the downloads page.

I highly recommend creating a separate folder, e.g., esap/, to house the source code you develop in this course.

Windows

The Windows command prompt, cmd.exe, operates somewhat differently from the Linux terminal although the text-based interface is similar. You can find a mapping between various commands on this old Redhat page. To open the Windows command prompt, you can “search” for cmd.exe or “Windows Command Prompt” from the search bar.

In addition to cmd.exe I recommend also downloading PuTTy to access the SEAS Unix servers.

OS X

(Currently this installation process has a 33% chance of working. If you have a mac, please wait on installing Pygame until we can figure it out.)

On OS X, you can use Terminal.app to get a Linux terminal (OS X is built upon a Unix core). You can open Terminal.app by using Searchlight (the magnifying glass in the top-right corner or apple key-space). On top of Python and Pygame, I recommend installing Homebrew to add additional command-line utilities that are not present on OS X by default.

Testing Your Environment

To ensure that your environment works, we’ll write the canonical “Hello World” program in Python—a program that simply prints the text hello world to the terminal. Navigate to your esap folder and create a text file called hello.py. In this file, add the following text:

print 'Hello World!'

As we shall see tomorrow, print is a statement in the Python programming language. It has the effect of printing the supplied text to the screen. This text is provided as an argument to the print statement; here we are printing the literal string 'hello world'.

Save this file and from the command prompt, you can run your program by passing it to the python interpreter, e.g.,

python hello.py

If you have done everything correctly, then you should see the text Hello World! printed to the console!

In addition to this, we’ll check to make sure that the Pygame library has been installed correctly. To do this, start up Python in interactive mode with the -i flag: python -i.

Rather than interpreting a Python source file, the interactive mode allows you to enter in Python statements which are then executed. The interactive mode is great for exploring the language; we’ll do so in the coming days. For now, we’ll just enter a few statement to test that Pygame is available:

import pygame

If Pygame is installed properly, issuing this statement should result in no output. Next, we can try running some of the sample games found in the examples module of the library. To run one of them, e.g., the Aliens example, enter the following statements:

import pygame.examples.aliens.main
pygame.examples.aliens.main()

If everything is installed correctly, you should be greeted with an Aliens game!

Turn-in

In this course, every lab that you do should be checked by a staff member. For this lab, please show the staff member, your:

  1. Webpage
  2. “Hello World!” program
  3. Working Pygame installation (demonstrated by playing one of the examples)