------------------------------------------------------------------------------
MC logo
Installing and Running Python
[^] CSc 232 Python, Fall, 2007
------------------------------------------------------------------------------

The simplest way to run programs for this course is use the Python system installed on Sandbox.

bennet 1000%python
Python 2.4.1 (#1, May 16 2005, 15:19:29) 
[GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello, there!"
Hello, there!
>>> joe = 10
>>> bill = 24
>>> print 3*(joe + bill)
102
>>> ^D
bennet 1001%
Python is terminated using control-D.

You can also edit a file with your favorite editor (pico, vi and emacs are available), and run your file with the python command.

bennet 1010%cat fred.py
fred = 17
joe = 99
print "fred + joe = ", fred + joe
bennet 1011%python fred.py
fred + joe =  116
bennet 1012%

You will often want to use the Unix shell redirects to read from or write to a file.

bennet 1036%python copy2.py
This program
This program
Simply echos
Simply echos
Whatever you type to it.
Whatever you type to it.
^D
bennet 1033%cat goober
This is a file.
Do you like it?
bennet 1034%python copy2.py < goober > newgoob
bennet 1035%cat newgoob
This is a file.
Do you like it?
bennet 1036%
These are very useful for command-line programming. The < character simply specifies that reading should come from a file (goober in this example) rather than the console. Likewise, > specifies a file for output (newgoob) instead of the console.

Installing On Linux

Python is standard equipment on most Linux distributions; if you have Linux, you almost certainly have Python. You can run Python programs on Linux using the python filename form as shown above. However, Linux, in common with other Unix-like systems, allows you run scripts by specifying the interpreter on the first line and marking the the file executable. This allows you place the script anywhere on your command path and run it as an ordinary command by just typing the file name. To do this, you must:

  1. Make the first line
    #!/usr/bin/python
  2. Make the file executable with the command
    chmod +x filename

Where /usr/bin/python is the location of your python interpreter, and filename is whatever you call your python file. Then, if filename is on your command path, filename becomes a command which just runs the python program.

Python comes with a small GUI development environment called Idle. It's not really needed on Linux, but can be useful. You can run it with a command like:

python /usr/lib/python2.4/idlelib/idle.py
Replace the 2.4 with whatever Python version you have. If you have a Red-Hat based system, you may also need to install the GUI interface for Python for this to work. yum install tkinter should do the trick.

Installing on Windows

To install Python on windows, download the official distribution from http://www.python.org/download/. The latest one as of this writing is 2.5.1. The Windows package is an MSI file that installs quite painlessly.

After installation is complete, you should have a Python entry in your start menu, with several submenu selections. The "python command line" submenu item will bring up a window into which you can interactively type python expressions. The "IDLE" selection will run the IDLE environment which allows you edit and run Python programs. You will find IDLE very useful on Windows. When IDLE comes up, you will see an interactive window where you can type Python expressions, much like the python command line selection. To open an editor window, use File/New Window. When you have entered a Python program and saved it, you can run it by selecting Run, then Run Module from the IDLE editor window. Your program will be run in the interactive window that originally came up when you started IDLE.

The Windows install binds the .py file extension to python, but this alone is often fairly useless for the text-based programs we'll write in this class. When you click on a python file, it opens a window, runs, and disappears. If you look quickly enough, you can just see it flash. If the program takes input, the window will stay up while you enter, then disappear just after the results are printed. IDLE solves this problem since the execution window stays open after you program finishes.

To follow some of the things we do in class, you will want to be able to run Python from a command window. To do this, you will need to add Python to the Windows execution path. The command path is just a list of directories where the system will look when trying to find a command, something like:

C:\windows;C:\dingle\bin
You must add the location of the python executable; if you did a default intall of version 2.5.1, you must add C:\Python25 to your path to get something like
C:\windows;C:\dingle\bin;C:\Python25
For XP, open the start menu, then right-click on the My Computer entry, and select properties. (For older NT-class machines, you right-click on the My Computer icon.) Choose the Advanced tab, and click the Environment Variables button near the bottom. Select PATH among the System Variables, click on Edit, and add C:\Python25 to the end. OK out to save your settings. You don't need to reboot, but the setting will only take effect for command windows started after the change.

These adjustments will allow you run Python files from a command prompt with python filename. The Unix style input and output redirection will also work in this environment.

Note: On Unix, interactive Python sessions are terminated with control-D. On Windows, use control-Z. These are keyboard EOF characters for the respective systems.