Chicago Python Workshop/Setup/OSX Setup
Welcome to the Friday evening section of the Chicago Python Workshop!
When you arrive, please start your laptop and get on the wireless network. Network information will be on the whiteboard. Once you join the network, work through the goals on this page in order; they should take between 2 and 3 hours. Lots of staff will be around to help troubleshoot and to answer questions -- don't hesitate to ask!
At the end, a staff member will go through a checklist with you to make sure you're all set for tomorrow. Please be sure to do this checkoff before you leave -- it'll make sure that you are in good shape for tomorrow.
Goal #1: set up Python
This section has instructions for installing Python and running Python from a command prompt.
OS X ships with Python installed, so the goal of this page is to make sure you can start a Terminal and run Python from the command line.
- Start up a Terminal. You can find the Terminal application through Spotlight, or navigate to Applications/Utilities/Terminal.
This Terminal contains something called a command prompt. This command prompt is another way of navigating your computer and running programs -- just textually instead of graphically. We are going to be running Python and Python scripts from this command prompt. - Test your Python install at the command prompt. Type
python
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. >>>
- You just started Python! The
>>>
indicates that you are at a new type of prompt -- a Python prompt. The command prompt let's you navigate your computer and run programs, and the Python prompt lets you write and run Python code interactively.
- If the Python version number (2.7.1 in the example above) is not a number between 2.4 and 2.7 (ignoring the number after the second dot), tell a staff member.
- You just started Python! The
- To exit the Python prompt, type
exit()
and press Enter. This will take you back to the OS X command prompt.
Success!
You have tested your Python installation.
Goal #2: prepare a text editor
In addition to being able to run Python, we are going to install a good text editor for writing and saving Python code during the workshop.
If you would like to use a different text editor from the recommendation for your operating system, please let a staff member know.
Our recommendation
On Mac OS, we suggest the Smultron text editor. You'll have to first download it, then install it.
To download Smultron:
- Click this link and download the ZIP file
- The ZIP file will expand into an application called Smultron.
- Open Finder from the bar at the bottom.
To install Smultron:
- Start Finder
- On the left side of the Finder, under "Places", click on "Applications".
- Drag the Smultron icon to any blank space in that window.
To run Smultron:
- 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 #3: practice starting and exiting Python
We'll do a lot of learning and practicing at a Python prompt (this is "interactive" because you are typing the code and hitting enter to run it yourself, instead of running it from a file). So let's 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 likejesstess$
).
Success!
Practice these steps until you feel comfortable navigating to a command prompt, starting Python, and exiting Python.
We will be running files containing Python code (Python "scripts") from the command prompt. You'll need to be able to navigate to those scripts using the command prompt so you can run them. In this section, we'll practice using these navigation commands.
The Filesystem
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 command prompt:
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 #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.
We are going to practice writing and running Python scripts.
Start your text editor
- Launch the Smultron text editor. See the OS X text editor setup instructions 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 command prompt. See the terminal navigation on OS X 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 typeexit()
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
, andcd
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 ofls
. - 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: get dependencies installed for the Saturday projects
Download and extract the Twitter project dependencies
- Click and save these four dependencies to your Desktop:
- http://mit.edu/jesstess/www/BostonPythonWorkshop6/Twitter/httplib2-0.6.0.zip
- http://mit.edu/jesstess/www/BostonPythonWorkshop6/Twitter/simplejson-2.1.6.zip
- http://mit.edu/jesstess/www/BostonPythonWorkshop6/Twitter/python-twitter.zip
- http://mit.edu/jesstess/www/BostonPythonWorkshop6/Twitter/python-oauth2.zip
- The ".zip" extension on the above files indicates that they are compressed Zip archives. We need to "extract" their contents. To do this, double-click on each file. This will create a directory for each file, containing the source code for the dependency.
Install the Twitter project dependencies
Each of these 4 dependencies has an installer script that we'll need to run at a command prompt to install the software. It is important that the dependencies are installed in the order listed above. For each project, start a command prompt and navigate to the Desktop directory where the source code lives. For example, if the httplib2-0.6.0 project was extracted to /Users/jesstess/Desktop/httplib2-0.6.0,
cd /Users/jesstess/Desktop/httplib2-0.6.0
will change you into that directory, and
ls
will show you the source code files in that directory. One of the files is "setup.py", which has a ".py" extension indicating that it is a Python script. Type:
sudo python setup.py install
and hit enter to install httplib2. You will need to enter your Mac account password.
Navigate to the 3 other dependency directories and run
sudo python setup.py install
in all of them to install those dependencies as well.
Download the Twitter project
We've written some skeleton code for the Twitter project already. Download this code so you're ready to start working with it tomorrow:
- Right click the following file, click "Save Target as..." or "Save link as...", and save it to your Desktop directory:
- Find Twitter.zip on your Desktop and double-click on it to "unzip" it. That will create a folder called Twitter containing several files.
Test the Twitter code
Start a command prompt and navigate to the Desktop/Twitter directory where the Twitter code lives. For example, if the Twitter project is at /Users/jesstess/Desktop/Twitter
,
cd /Users/jesstess/Desktop/Twitter
will change you into that directory, and
ls
will show you the source code files in that directory. One of the files is "twitter_api.py", which has a ".py" extension indicating that it is a Python script. Type:
python twitter_api.py --search=python
at the command prompt to execute the twitter_api.py Python script. You should the text from 20 tweets containing the word "Python" printed to the screen. If you don't, let a staff member know.
Success!
You've completed setup for the Twitter project.
State Capitals
We'll look at an example Python script that quizzes you on state capitals during the lecture on Saturday.
- Right click the following file, click "Save Target as..." or "Save link as...", and save it to your Desktop directory:
Success!
You are done installing dependencies for the Saturday projects.
Goal #7: start learning Python!
It's time to start learning, reading, and writing some Python! Tonight, you'll work through a self-directed tutorial. Saturday morning, we'll have an interactive lecture to cover more Python basics.
Goal #8: practice Python using CodingBat
Learning about functions opened up a whole new way for us to practice, using the programming site codingbat.com. The big goal of this practice section is to get you thinking about how to solve problems in Python.
You don't have to create a CodingBat account to do the exercises, but if you do create an account it'll keep track of which problems you've completed and other statistics.
Goal #9: Checkoff
Tell a staff member that you are ready to be checked off. Together you will go through the following check-off steps:
- Start a command prompt, and from that command prompt start Python. Then quit Python.
-
Create a new Python file (with a .py extension). In that file, type
print "Hello World"
and save the file. From a command prompt, navigate to and execute that Python script.
- Open your text editor, and press "Tab". Use the left arrow key to show the instructor that you are using spaces to indent, not tabs.
- To test the Twitter installation, navigate to the Twitter directory and run
twitter_api.py
:python twitter_api.py --search=python
- Walk through the CodingBat problem that you had the most difficulty with. The Friday CodingBat questions are here.
If you have any other questions about the tutorial, project setup, or CodingBat questions, now is a great time to ask!
Congratulations!
You are done with the Friday portion of this Workshop. We'll see you back here at 9:30am tomorrow! Please bring the laptop you used tonight.
If you have any questions, comments, or feedback on tonight's material, don't hesitate to let a staff member know.