Working with Python

Although Python is available for multiple platforms, the procedure to execute a Python script depends greatly on the host system. Below are some suggestions on how one might go about executing Python programs on Linux, Macs and Windows. These suggestions are useful for people who prefer to use their own text editors for modifying Python source code and then execute the files for testing. A separate document describes how one might use the Python interactive development environment (IDLE) for both writing and running Python programs.

Using Python on Linux

Python comes standard with Linux.

To run a Python script, we need to start a Terminal window and type:

python filename.py

We can either supply the full path to the file, or use just the file name if we have changed to the directory where the file sits. The output of executing the script will be displayed in the same Terminal window.

If the file name argument is omitted, the Python interpreter is run in interactive mode. To exit interactive mode, type control-D.

Using Python on Mac OS X 10.4

Mac OS X 10.4 is shipped with the Python 2.3 distribution, which includes both the actual Python interpreter and some helper applications. You can also get the latest version from the Python download site and follow the installation instructions.

Suppose we start with a file named t.py whose content is the single line

print "hello world"
You might think that double-clicking on the t.py icon should execute it. However, what actually happens is that we get a new app t.app) instead. If we now double-click on the t.app icon, something appears briefly in the dock and then disappears. What happened is that the script executed and finished. The output may be found in the system console (which can be examined using Utilities/Console.app). Having to convert a script to an app, execute the app, and then search for the output is not an efficient way of writing and testing programs. Fortunately, there is an easier way. If we do a Get Info on t.py, we see:

Note that OS X wants to open the file using BuildApplet.app. This is the program that converts our script into an app. Note also that PythonLauncher.app is an alternative. If we choose PythonLauncher as our opener app, and then double-click the t.py icon, we see that a new Terminal window pops up, and our output "hello world" is sandwiched between some stuff:

% "/usr/bin/python"  "/Users/conrad/Desktop/t.py"  && echo Exit status: $? && exit 1
hello world
Exit status: 0
logout
[Process exited - exit code 1]

To make double-clicking on any Python script behave this way, we can click on the Change All... button in the Open with: section.

Of course, we can start a Terminal ourselves and run Python the same way as on Linux (see above).

NOTE:The suggestions above are for OS X 10.4. Earlier versions of OS X do not support the Tk user interface library natively, so programs such as "idle" are not available.

Using Python on Microsoft Windows

Python does not come standard on Windows, so you will need to install it by fetching the installer from the Python download site. Once installed, the Python files are usually found in c:\Python24 or something similar, depending on the version.

Suppose we start with a file named t.py whose content is the single line

print "hello world"
Double-clicking on the t.py icon executes it. Unfortunately, the output appears in a window that immediately disappears. To see the output, we need to start a command window by selecting the Run... item in the Start menu and typing in
cmd

This should pop up a command window. We can then run our program by typing:

c:\python24\python filename.py

We can either supply the full path to the file, or use just the file name if we have changed to the folder where the file sits. The output of executing the script will be displayed in the same command window.

If the file name argument is omitted, the Python interpreter is run in interactive mode. To exit interactive mode, type control-Z followed by Enter.

To avoid having to type the c:\python24\ part constantly, we can put that folder on our execution path with:

set path=%path%;c:\python24

Once the Python folder is on our execution path, we can just type:

python filename.py