Chicago Python Workshop/Setup/Windows Setup

From OpenHatch wiki
Revision as of 19:59, 7 December 2012 by imported>Codersquid (Codersquid moved page Chicago Python Workshop/Windows Setup to Chicago Python Workshop/Setup/Windows Setup)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Download and install Python

If you believe you already have Python installed, please let a staff member know before completing these steps.

  1. Click http://python.org/ftp/python/2.7.1/python-2.7.1.msi and choose "run" if you have the option to. Otherwise, save it to your Desktop, then minimize windows to see your desktop, and double click on it to start the installer. Follow the installer instructions to completion.
  2. Open a command prompt (we will be doing this multiple times, so make a note of how to do this!):
    • On Windows Vista or Windows 7: click on the Start menu (the Windows logo in the lower left of the screen), type cmd into the Search field directly above the Start menu button, and click on "cmd" in the search results above the Search field.
    • On Windows XP: click on the Start menu (the Windows logo in the lower left of the screen), click on "Run...", type cmd into the text box, and hit enter.

    You now have what's 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.
  3. At this C:\ or C:\Users\<username> prompt that appears, test your Python install by typing
    \Python27\python.exe
    

    and hitting enter. You should see something like

    Python 2.7.1 (r271:86832, ...) on win32
    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.

  4. To exit the Python prompt, type
    exit()
    
    and press Enter. This will take you back to the Windows command prompt (the C:\ you saw earlier).

Put Python on the PATH

You might have noticed that you typed a "full path" to the Python application above when launching Python (python.exe is the application, but we typed \Python27\python.exe). In this step, you will configure your computer so that you can run Python without typing the Python27 directory name.

Get to System Properties

  1. Open up "My Computer" by clicking on the Start menu or the Windows logo in the lower-left hand corner, and navigate to "My Computer" (for Windows XP) or "Computer" (For Vista and Windows 7).
  2. Right-click on the empty space in the window, and choose Properties.
If you're using XP

A window labeled "System Properties" will pop up.

  1. Click the "Advanced" tab.
If you're not using XP

A window labeled "View basic information about your computer" will appear.

  1. In this window, click "Advanced system settings"

A window with the title "System Properties" will appear.

Edit the Path

  1. Within System Properties, make sure you are in the tab labeled "Advanced".
  2. Click the button labeled "Environment Variables".
    • A window labeled "Environment Variables" will appear.
  3. In this window, the screen is split between "User variables" and "System variables". Within "System variables", scroll down and find the one labeled Path. Click the "Edit..." button.
    • A window with the "Variable name" and the "Variable value" should appear. The "Variable value" will already have some text in it; click in the box to unhighlight it (we don't want to accidentally delete that text).
  4. In the "Variable value" box, scroll to the end. Add the following text, and hit OK. Make sure to include the semicolon at the start!
    ;c:\python27\;c:\python27\scripts
  5. Hit "OK" to close out the system properties window.
  6. Test your change:
    1. Open up a new command prompt: you do this the same way you did above when installing python. This needs to be a new command prompt because the changes you just made didn't take affect in prompts that were already open.
    2. Type python into the command prompt to start Python
    3. Notice that you now get a Python interpreter, indicated by the change to a >>> prompt.
    4. Exit the Python prompt by typing
      exit()
      and hitting enter. Now you're back at the Windows command prompt (C:\).

Success!

You have Python installed and configured.

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 Windows, we suggest the Notepad++ text editor. It is actually unrelated to the Windows default program called Notepad.

To install it:

To run it:

  • In Windows Vista or Windows 7, click on the Start menu, type Notepad++ in the Search Field, and hit enter.
  • In Windows XP, click on the Start menu, navigate to All Programs, and then navigate to Notepad++.

Configure Notepad++ to indent with spaces

  • Click Settings -> Preferences
  • Find the tab labeled "Language Menu/Tab Settings"
  • Find the box labeled "Replace by space", and make sure it is checked
  • Click Close.

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 have 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:

  1. Open a command prompt:
    • On Windows Vista or Windows 7: click on the Start menu (the Windows logo in the lower left of the screen), type cmd into the Search field directly above the Start menu button, and click on "cmd" in the search results above the Search field.
    • On Windows XP: click on the Start menu (the Windows logo in the lower left of the screen), click on "Run...", type cmd into the text box, and hit enter.
  2. To start Python, type
    python
    at the command prompt and hit enter. You should see something like
    Python 2.7.1 (r271:86832, ...) on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

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

  3. Exit the Python prompt by typing
    exit()
    and hitting enter. Now you're back at the Windows command prompt (C:\).

Success!

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

Goal #4: practice navigating the computer from a command prompt

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 two commands that we'll be using at a command prompt to navigate the filesystem on your computer:

  • dir
  • cd

dir lists the contents of a directory.
cd moves you into a new directory (it stands for "change directory").

Let's practice using these commands.

Open a command prompt:

  • On Windows Vista or Windows 7: click on the Start menu (the Windows logo in the lower left of the screen), type cmd into the Search field directly above the Start menu button, and click on "cmd" in the search results above the Search field.
  • On Windows XP: click on the Start menu (the Windows logo in the lower left of the screen), click on "Run...", type cmd into the text box, and hit enter.

Practice using dir and cd

Type each of these commands and hit enter:

dir

This lists all the files in your home directory.


cd C:\

This will change you into the C:\ directory.


dir

This lists the contents of the C:\ directory.


If you're using Windows XP or an earlier version

cd Documents and Settings

This will change you into the Documents and Settings subdirectory of the C:\ directory.


dir

You should see the names of all the files and directories in C:\Documents and Settings.


cd ..

.. means "parent directory", so this command moved you up to the parent directory. You were in C:\Documents and Settings, so now you are in C:\, the root directory.


dir

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

If you're using Vista or Windows 7

cd Users

This will change you into the Users subdirectory of the C:\ directory.


dir

You should see the names of all the files and directories in C:\Users.


cd ..

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


dir

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 Use and hit Tab, the command prompt will auto-complete the directory name, and you can then hit enter to change into the C:\Users directory.
  • The command prompt maintains a command history. You can use the up arrow to cycle through old commands.
  • Note that the text that makes up the command prompt changes as you move around directories. The command prompt will always give the full directory path to your current directory.

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.

  1. What directory are you in after starting a new command line prompt?
  2. After starting a new command line prompt, how would you get to the root directory?
  3. How do you check what files and directories are in your current working directory?
  4. (Win XP or earlier) If you are in directory C:\Documents and Settings, and you want to get to C:\Documents and Settings\<username>\projects, how would you do that?
  5. (Vista or Win 7 only) If you are in directory C:\Users , and you want to get to C:\Users\<username>\projects, how would you do that?
  6. What are 2 ways to avoid typing out a full navigation command? (hint: one requires that you've run the command before)
  7. What is the difference between a command prompt and a Python prompt?

Success!

You've practiced using dir 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

  1. Launch the Notepad++ text editor. See the Windows text editor setup instructions for the steps to do this.
  2. Start a new, blank text file.

Write and save a short Python script

  1. Add the following line to your new text file:
print "Hello World!"
  1. Saving the script
    1. Click either the Save button or going to File > Save
    2. Then, navigate to your Desktop folder, which will be under C:\Documents and Settings\<username> or C:\Users\<username>.
    3. Once you've double clicked on the Desktop folder, then click the new folder icon. Then name it python. This is where we will keep all our python code and projects, so that it's easy to find.
    4. Then click through the python folder you just created, and save the file as hello.py in this python directory. The .py extension indicates that this file contains Python code.

Run the script

  1. Start a new command prompt. See the terminal navigation on Windows instructions for the steps to do this. Recall that a terminal prompt will look like C:\ 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.
  2. Navigate to your python directory, which is in your desktop directory, from a command prompt, using the dir and cd commands. See the terminal navigation on Windows 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!
  3. Once you are in your python directory, you'll see hello.py in the output of dir.
  4. 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

Twitter

Download and extract the Twitter project dependencies

  1. Click and save these four dependencies to your Desktop\python directory:
  2. The ".zip" extension on the above files indicates that they are compressed Zip archives. We need to "extract" their contents. To do this, click on "Start", then "Computer", navigate to your Desktop directory, and finally to the python directory. For each of the 4 zip files, click on the file and click the "Extract all files" button to extract the contents. This will create a directory for each file, containing the source code for the dependency.

    NOTE: if the last package fails to install, but complains about needing setuptools, you should download and install this file: Just save this file to your computer then double click on it to run the installer. For full instructions and links to versions for other versions of Python, go to this link http://pypi.python.org/pypi/setuptools/#windows

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 C:\Users\jesstess\Desktop\httplib2-0.6.0,

cd C:\Users\jesstess\Desktop\python\httplib2-0.6.0

will change you into that directory, and

dir

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:

python setup.py install

and hit enter to install httplib2.

Navigate to the 3 other dependency directories (in the order you downloaded them: simplejson-2.1.6, python-twitter, python-oauth2) and run

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:

  1. Right click the following file, click "Save Target as..." or "Save link as...", and save it to your Desktop directory:
  2. 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 C:\Users\jesstess\Desktop\python\Twitter (or C:\Documents and Settings\jesstess\Desktop\python\Twitter for Win XP or earlier),

for Win Vista or Win 7

cd C:\Users\jesstess\Desktop\python\Twitter

or for Win XP users or earlier:

cd C:\Documents and Settings\jesstess\Desktop\python\Twitter

will change you into that directory, and

dir

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.

  1. 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:

  1. Start a command prompt, and from that command prompt start Python. Then quit Python.
  2. 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.

  3. 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.
  4. To test the Twitter installation, navigate to the Twitter directory and run twitter_api.py:
    python twitter_api.py --search=python
    
  5. 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.