Go to: Course home page  

Lab FAQ

Frequently Asked Questions

  1. How do I install Python? [ANSWER]
  2. What is Portable Python? How do I use it?[ANSWER]
  3. Accessing command history: How do I quickly bring up a previously entered command? [ANSWER]
  4. I dislike using "Control-p" and "Control-n" keys for command history. Can I use UpArrow and DownArrow instead like in most other shell environments? [ANSWER]
  5. How do I save my IDLE session into a file?[ANSWER]
  6. How do I take a screen shot?[ANSWER]
  7. Where is my Python installed? [ANSWER]
  8. What text editing programs should I use? [ANSWER]
  9. (Mac) IDLE prints a warning message about the Tcl/Tk version. How can I fix this? [ANSWER]
  10. (Win) IDLE "starts in" C:\Python27 by default and saves all my scripts there. How do I change this behavior?[ANSWER]
  11. What is the difference between Mac/Linux file path and Windows file path? [ANSWER]
  12. I am trying out these commands that I found on a Python tutorial site, but I keep getting an error. What is wrong? [ANSWER]
  13. I am having trouble reading/writing/loading a particular file. [ANSWER]
  14. How do I work with CWD (current working directory) in Python shell? [ANSWER]
............................................................................................................
  1. How do I install Python?
    You need Python version 2.7. The latest version is 2.7.6. Download an appropriate package for your OS from Python's official download page (http://www.python.org/download/releases/2.7.6/) and install it.

  2. What is Portable Python? How do I use it?
    A portable application is a software title that you can start using after downloading without going through an installation process. This means you can use the software on a computer where you don't have an administrator's right (such as Pitt's computer lab machines). Also, you can take it with you by storing on a USB flash disk and use it on any machine.
    Portable Python is compatible with any PC. You can download it here: http://portablepython.com/wiki/PortablePython2.7.5.1/ (106 MB).

  3. Accessing command history: How do I quickly bring up a previously entered command?
    IDLE's default key bindings for command history are Ctrl-p for a previously entered command and Ctrl-n for the next command in the command history. You might find that these keys are not exactly handy -- see this FAQ for how to assign different keyboard shortcuts.

  4. I dislike using "Control-p" and "Control-n" keys for command history. Can I use UpArrow and DownArrow instead like in most other shell environments?
    Frankly, Ctrl-p and Ctrl-n are a pain to use. But you can customize keyboard shortcuts in IDLE -- follow the steps below to assign UpArrow and DownArrow to "previous command" and "next command" instead:
    • Open up IDLE, from the menu go Options --> Configure IDLE. Click Keys tab.
    • Scroll down and click on the line starting with "history-next". Click button "Get New Keys for Selection".
    • Scroll down to find "Down Arrow" and click on it. The new key is now set to "<Key-Down>". Press OK. [screenshot]
    • You are prompted to name your custom key scheme. Give any name.
    • Now repeat above process for "history-previous". But this time select "Up Arrow". [screenshot]

  5. How do I save my IDLE session into a file?
    From the menu, choose File -> Save As, and then give the file name. Make sure to use the .txt extension and not .py: a saved IDLE session should be a text file and not a Python script file.

  6. How do I take a screen shot?
    Windows:
    • Method 1: Alt-PrtScn (copies the active screen onto clipboard) and then Ctrl+V (pastes onto, say, a Word Document).
    • Method 2: In Windows 7, you can use "Snipping Tool" under "Accessories" folder. It gives an option to capture a particular portion of a window and also to save image as a file.
    • Method 3: Use an excellent free application called Greenshot. It lets you annotate your image.
    Mac OS-X:
    • Method 1: To capture a portion of the desktop, press Command-Shift-4. A cross-hair cursor will appear and you can click and drag to select the area you wish to capture. When you release the mouse button, the screen shot will be automatically saved as a PNG file on your desktop. (The file is saved as PDF in Mac OS 10.3 and earlier.) [source]
    • Method 2: To capture a specific application window, press Command-Shift-4, then press the Spacebar. The cursor will change to a camera, and you can move it around the screen. As you move the cursor over an application window, the window will be highlighted. The entire window does not need to be visible for you to capture it. When you have the cursor over a window you want to capture, just click the mouse button and the screen shot will be saved as a PNG file on your desktop. (The file is saved as PDF in Mac OS 10.3 and earlier.) [source]
    • Method 3: Add Control to the two shortcuts above to place the screen shot on the clipboard instead of saving it to the desktop. [source]

  7. Where is my Python installed?
    • Windows standard installation: The default location is C:\Python27.
    • OS-X: /Library/Frameworks/Python.framework/Versions/2.7
    • Portable Python: It's the Portable Python 2.7.5.1 directory, where ever you put it. If it's on your USB thumb drive, it is likely D:\Portable Python 2.7.5.1 or E:\Portable Python 2.7.5.1.

  8. What text editing programs should I use?
    For Windows, Notepad++ and JEdit are highly recommended. For Mac, try TextWrangler or JEdit.

  9. (Mac) IDLE prints a warning message about the Tcl/Tk version. How can I fix this?
    If you are getting a warning message like this, then the source of the problem is your outdated TCL version. Follow the instructions on this page to fix it.

  10. (Win) IDLE "starts in" C:\Python27 by default and saves all my scripts there. How do I change this behavior?
    INSTRUCTIONS

  11. What is the difference between Mac/Linux file path and Windows file path?
    OS-X/Linux file paths look like this: /Users/narae/Desktop/foo.txt.
    They start from the root "/", and slashes are used to separate directories.

    Windows file paths look like this: C:\Users\narae\Desktop\foo.txt.
    They start with the disk label "C:", and backslashes are used to separate directories. Because the backslash is a special character in Python, when you refer to a Windows file path as a string in Python you must remember to either escape your backslashes: 'C:\\Users\\narae\\Desktop\\foo.txt' or prefix it with the rawstring marker "r" : r'C:\Users\narae\Desktop\foo.txt'.

  12. I am trying out these commands that I found on a Python tutorial site, but I keep getting an error. What is wrong?
    It is likely that your Python tutorial site is based on a newer version of Python: Python 3. Many changes were introduced in Python 3.X.X, most notable of which is the print command. While Python 3 is gaining in adoption, a lot of modules and applications have not yet fully migrated to this platform, including NLTK, which is the reason why we are using Python 2.7 in this class.

  13. I am having trouble reading/writing/loading a particular file.
    If you are getting a "No such file or directory" error (example here), that is because Python cannot locate your file. The concept of Current Working Directory (CWD) is crucial here. Basically, referring to a file without specifying its path ('myfile.txt') works only when the file is in your CWD. To complicate the matter, your Python has different initial CWD settings depending on whether you are working with a Python script or in a shell environment.
    • Working with a Python script:
      When you run your script, your CWD is set to the directory where your script is. You have two options:
      1. Keep your file where it is, and then refer to it in the script with its full file path and name, e.g., '/Users/narae/Desktop/myfile.txt' (Mac) or r'D:\Lab\scripts\myfile.txt' (Windows. Since '\' is a special character, use 'r' raw-string prefix).
      2. Move or copy your file to where your script is and refer to it in the script with its name only, e.g., 'myfile.txt'.
    • Working in Python shell:
      In your shell, the initial CWD setting varies by system. You have three options:
      1. Keep your file where it is, and then refer to it with its full file path and name, e.g., '/Users/narae/Desktop/myfile.txt' (Mac) or r'D:\Lab\scripts\myfile.txt' (Windows. Since '\' is a special character, use 'r' raw-string prefix).
      2. Change your CWD and refer to your file with its name only, e.g., 'myfile.txt'. See here and here.
      3. Copy or move your file to your CWD. (Not recommended, since your shell's CWD may change)
      See this screen shot and this FAQ for how to work with your CWD setting in Python shell. A final note: if you are working in IDLE, executing a script changes your IDLE shell's CWD to your script's directory.

  14. How do I work with CWD (current working directory) in Python shell?
    Python module os provides utilities for that. Below illustrates how to find your CWD (.getcwd()) and change it into a different directory (.chdir()). Below is an example for the windows OS:
     
    >>> import os
    >>> os.getcwd()
    'D:\\Lab'
    >>> os.chdir(r'scripts\gutenberg') # relative path: scripts directory is under Lab
    >>> os.getcwd()
    'D:\\Lab\\scripts\\gutenberg'
    >>> os.chdir(r'D:\Corpora\corpus_samples') # absolute path
    >>> os.getcwd()
    'D:\\Corpora\\corpus_samples'
    On a Mac, your file path should look like '/Users/narae/Desktop/'.