Chicago Python Workshop/Setup/Linux 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.

Linux 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.

  1. Start up a Terminal. You can find the Terminal application at Applications/Accessories/Terminal, or it may already be on your menu bar.

    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.
  2. Test your Python install at the command prompt. Type
    python
    

    and hit enter. You should see something like

    Python 2.7.1 (r252:60911, Jan 24 2011, 17:44:40) 
    [GCC 4.3.2] on linux2
    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.
  • To exit the Python prompt, type
    exit()
    
    and press Enter. This will take you back to the Linux command prompt.
  • You might need to install a package called python-tk.

    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.

    How to find gedit in the Applications list

    On Linux, we suggest the GEdit text editor.

    GEdit should already be installed on your computer. To start GEdit: click Applications, point to Accessories, and click Text Editor. If you don't have this option, let a staff member know.

    How to find GEdit from a terminal

    If you prefer to start applications from a terminal, you can type the following into a terminal to launch GEdit:

    gedit &
    

    Configure gedit to indent with spaces

    • Click Edit -> Preferences
    • Select the tab labeled "Editor"
    • In the "Tab width" field, set it to 4 (the default is 8)
    • Check the box labeled "Insert spaces instead of tabs"
    • Check the box labeled "Enable automatic indentation"
    • 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!

    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:

    Linux 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.

    1. Open a Terminal command prompt. You can find the Terminal application at Applications/Accessories/Terminal, or it may already be on your menu bar.
    2. To start Python, type
      python
      

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

      Python 2.7.1 (r252:60911, Jan 24 2011, 17:44:40) 
      [GCC 4.3.2] on linux2
      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 Linux command prompt (which looks something like jesstess$).

    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 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 at Applications/Accessories/Terminal, or it may already be on your menu bar.

    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 home

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


    ls

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


    pwd

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


    cd ..

    .. means "parent directory", so this command moved you up to the parent directory. You were in /home, 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 ho and hit Tab, the command prompt will auto-complete the directory name, and you can then hit enter to change into the /home 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.

    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. If you are in directory /home, and you want to get to /home/jesstess/projects, how would you do that?
    5. What are 2 ways to avoid typing out a full navigation command? (hint: one requires that you've run the command before)
    6. 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

    1. Launch the GEdit text editor. See the Linux 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. Save the script as hello.py in your home directory. The .py extension indicates that this file contains Python code.

    Run the script

    1. Start a command prompt. See the terminal navigation on Linux 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.
    2. Navigate to your home directory from a command prompt, using the ls, pwd, and cd commands. See the terminal navigation on Linux 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 home directory, you'll see hello.py in the output of ls.
    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

    Download and extract the Twitter project dependencies

    1. Click and save these four dependencies to your Desktop:
    2. 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 /home/jesstess/Desktop/httplib2-0.6.0,

    cd /home/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
    

    type in your password, and hit enter to install httplib2.

    Navigate to the 3 other dependency directories and run

    sudo python setup.py install
    

    in all of them to install those dependencies as well.

    If you get an error like:

    ImportError: No module named setuptools
    

    you need an extra package. Type:

    sudo apt-get install python-setuptools
    

    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.tar.gz on your Desktop and double-click on it to "extract" 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 /home/jesstess/Desktop/Twitter,

    cd /home/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 see 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.