Goal #1: practice navigating the computer from the terminal

The filesystem on your computer is like a tree made up of folders (also called "directories") and files. The filesystem has a root directory called /, and everything on your computer lives in subdirectories of this root directory.

We often navigate the filesystem graphically by clicking on graphical folders. We can do the exact same navigation from the command line.

There are three commands that we'll be using at a command prompt to navigate the filesystem on your computer:

ls
pwd
cd

ls lists the contents of a directory. pwd gives the full directory path to your current directory. cd moves you into a new directory (it stands for "change directory").

Let's practice using these commands.

Open a Terminal

You can find the Terminal application through Spotlight, or navigate to Applications/Utilities/Terminal.

Practice using ls, pwd, and cd

(that's an l the letter, not the number 1)

Type each of these commands and hit enter:

ls

This lists all the files in your home directory.

pwd

This displays the full directory path to your current directory, which is your home directory.

cd /

This will change you into the / root directory.

ls

This lists the contents of the / root directory.

cd Users

This will change you into the Users subdirectory of the / root directory.

ls

You should see a list of all the files in /Users, including the directory for your username -- your home directory.

pwd

This displays the full directory path to your current directory, /Users.

cd ..

.. means "parent directory", so this command moved you up to the parent directory. You were in /Users, so now you are in /, the root directory.

ls

This lists the contents of the root directory, confirming where you are.

Tips

You can use Tab to auto-complete directory and file names. So from inside the root directory /, if you type cd Us and hit Tab, the command prompt will auto-complete the directory name, and you can then hit enter to change into the /Users directory.

The command prompt maintains a command history. You can use the up arrow to cycle through old commands.

Check your understanding

Answer these questions. Experiment at the command line if you need to! If you aren't sure about an answer, ask a helper.

  • What directory are you in after starting a new command line prompt?
  • After starting a new command line prompt, how would you get to the root directory?
  • How do you check what files and directories are in your current working directory?
  • If you are in directory /Users, and you want to get to /Users/jesstess/projects, how would you do that?
  • What are 2 ways to avoid typing out a full navigation command? (hint: one requires that you've run the command before)
  • What is the difference between a command prompt and a Python prompt?

Success!

You've practiced using ls, pwd, and cd to navigate your computer's filesystem from the command prompt.

Goal #2: Install needed software

Insert the CD or USB drive provided by an instructor. In Finder, drag the icon for the "workshop" folder onto your desktop to make your own copy of the class materials. Then you can remove the CD or USB drive and pass it along. If you can't use a CD or USB drive, download this file and have an instructor help you prepare it.

Install Anaconda Python

The Python programming language is "open-source" - no single person or company owns it, or can own it - it's for everyone to use. Some companies wrap it up together with useful related programs. We'll use one such packaging called Anaconda, published free of charge by Continuum Analytics, because that's the most reliable way to get everything we need installed.

Double-click the "workshop" folder you just copied to enter it, then enter the "mac" folder.

If your computer is using OS X 10.7 or higher: Double-click "Anaconda-1.7.0-MacOSX-x86_64.pkg" and accept all the defaults to install Python.

If your computer is using OS X 10.5 or 10.6: Double-click "Anaconda-1.7.0-MacOSX-x86_64.sh" and accept all the defaults to install Python.

If you don't know: I know, that's not a beginner-level question! So try Anaconda-1.7.0-MacOSX-x86_64.pkg first, and if that dosn't work, try Anaconda-1.7.0-MacOSX-x86_64.sh.

Install Smultron

Next, install Smultron, a text editor for writing programs:

  • Enter the "smultron" subfolder within the "workshop" folder you unzipped
  • On the left side of the Finder, under "Places", click on "Applications".
  • Drag the Smultron icon to any blank space in that window.

Everything we're installing is free and open-source.

Goal #3: practice starting and exiting Python

Start up a Terminal command prompt. You can find the Terminal application through Spotlight, or navigate to Applications/Utilities/Terminal.

To start Python, type

python

at the command prompt and hit enter. You should see something like

Python 2.7.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> indicates that you are at a Python prompt.

Exit the Python prompt by typing

exit()

and hitting enter. Now you're back at the OS X command prompt (which looks something like jesstess$).

Check your understanding

What is the difference between a command prompt and a Python prompt? If you're not sure, ask a helper.

Success!

Practice these steps until you feel comfortable navigating to a command prompt, starting Python, and exiting Python.

Goal #4: Configure your text editor

Our recommendation

Start the Smultron editor we installed in Goal #2:

  • Start Finder
  • On the left side of the Finder, under "Places", click on "Applications".
  • In the middle column, scroll down to to find Smultron.
  • Double-click Smultron to launch the editor.

If you run into trouble with Smultron

If you can't run the version of Smultron we recommend, try the older version 3.1.2 of Smultron. We've tested that it runs properly on Mac OS 10.4 on PowerPC and Intel.

Configure Smultron to indent with spaces

  • Start up Smultron, and click Smultron -> Preferences. This will pop up a preferences window.
  • Click on the Advanced tab, and then on the Really Advanced tab within that tab.
  • Check the "Indent with spaces, not tabs" checkbox
  • Close the Preferences window.

That's it! Now, you can hit tab to indent your code, and that indentation will actually be made of spaces. This change will help you use spaces consistently, so that Python doesn't get confused about whitespace.

Success!

Now you have an editor that you can use to open any text file, including Python programs.

If you prefer a different editor for text, check with an instructor before moving on to make sure it will work for the weekend.

Goal #5: practice running Python code from a file

Interactive Python programming at a Python prompt is great for short pieces of code and for testing ideas. For longer code, it can be easier to save the code in a file, and execute the contents of that file (aka a Python script). In this section, we'll practice running Python scripts.

Start your text editor

  • Launch the Smultron text editor. See Goal #4 for the steps to do this.
  • Start a new, blank text file.

Write and save a short Python script

Add the following line to your new text file:

print "Hello World!"

Save the script as hello.py in your home directory. The .py extension indicates that this file contains Python code.

Run the script

  • Start a new command prompt. See Goal #1 for the steps to do this. instructions for the steps to do this. Recall that a terminal prompt will look like jesstess$ and a Python prompt will look like >>>. Make sure you are at a terminal prompt and not a Python prompt; if you are at a Python prompt, you can type exit() on a line by itself and then hit enter to exit Python and return to a terminal prompt.
  • Navigate to your home directory from a command prompt, using the ls, pwd, and cd commands. See the terminal navigation on OS X instructions for a refresher on using these commands. Don't hesitate to get help from a staff member on this step if you need it -- it's a new way of navigating your computer, so it may be unintuitive at first!
  • Once you are in your home directory, you'll see hello.py in the output of ls.
  • Type

    python hello.py

and hit enter. Doing this will cause Python to execute the contents of that script -- it should print "Hello World!" to the screen. What you've done here is run the Python application with an argument -- the name of a file, in this case "hello.py". Python knows that when you give it a file name as an argument, it should execute the contents of the provided file. You get the same result as if you typed

print "Hello World!"

at a Python prompt and hit enter.

Success

You created and ran your first Python script!

  • When you run the python command by itself, you start a Python prompt. You can execute Python code interactively at that prompt.
  • When you run the python command with a file name as an argument, Python executes the Python code in that file.

Goal #6: Practice using IPython Notebook

You'll spend most of this workshop using a Python tool called the IPython Notebook. This lets you run IPython cells, which are like the interactive Python prompt you tried out in Goal #3, right within your browser.

  1. Open a Terminal (see Goal #1) and use cd to navigate into the workshop directory.

  2. Type

    ipython notebook

    and hit enter.

  3. Your browser should come up with a page called "IP[y]: Notebook". That's your IPython Dashboard; it has a list of notebooks. Click "notebook_practice" and follow the instructions on the notebook that it brings up.

Goal #7: Tutorial

Go back to the IPython Dashboard page in your browser, and click the "day1.1" notebook. Follow along through that tutorial. Remember to ask lots of questions, to the instructors and to the other students - we're all here to help each other!

After "day1.1", you'll go through "day1.2" and then part of "exercises". We'll leave the rest for tomorrow!