Skip to content

Loading and Running Existing Applications

Loading and Running Existing Applications

Running your application immediately after you write it is fun and interest- ing, but at some point you’ll close IDLE and be left with a file on your disk. The file contains your application, but you need to know how to use that file to execute it. Python actually provides a considerable number of ways to achieve this task. The following sections describe just three of these approaches.

 

 

Using the command line or terminal window

The command line, or terminal window, provides the means to execute com- mands by typing them in. You can also create batch files to execute a number of commands as part of a batch process. In this case, you’re looking at the native command environment provided by the platform you’re using, rather than at the specialized Python command line. When working in this environ- ment, you type commands to start Python and perform specific tasks. For example, if you want to execute FirstApp (described in the “Creating the Application” section of this chapter), you type python FirstApp.py and press Enter. Figure 4-24 shows typical results. You can execute any other applica- tion this way as well.

 

 

 

 

 

 

 

 

Figure 4-24: It’s possible to execute an applica­ tion directly at the com­ mand line.

 

 

 

Using the Edit window

Any time you’re in IDLE, you can open an existing application in an Edit window and execute it, just as you have in previous sections of this chapter. To perform this task, load the file you saved earlier by choosing File➪Open. You see an Open dialog box that looks similar to the Save As dialog box shown in Figure 4-14. Choose the folder containing the application in the  Look In field and highlight it in the list provided. Click Open to open the file. At this point, you can choose Run➪Run Module to run the application, just as you would normally.

 

 

Using the Python Shell window or Python command line

When you’re in the IDLE Python Shell window or at the Python command line, you’re in an environment where you can type commands and see them executed immediately. However, you need to know the right commands to perform specific tasks. In this case, the command is a little more complex than the print() command you’ve been using to date. If you want to exe- cute FirstApp, you need a really odd-looking command like one of the two shown here:

 

 

The preceding two commands are really the same one using a different type of slash. The command works equally well with forward slashes or backslashes. What this command says to do is this:

 

  1. Open the py file located in the \BP4D\Chapter04 folder on the C drive (open() command).
  2. Read the content of this file into the Python environment (read() command).
  3. Execute the instructions found in the file after it’s loaded (exec() command).

 

It’s a little early for a command like this one, but you’ll discover that you can create combined commands of all sorts later in the book. For now, just try the command to see that it works. Figure 4-25 shows typical results.

 

 

 

Figure 4-25: Use forward slashes or backslashes

to define the loca­ tion of your application.

 

 

Closing IDLE

Eventually, you need to close IDLE when your session is finished. The com- mands for closing IDLE appear on the File menu, and there are actually two of them (which seems a bit confusing):

 

  • Close: Closes just the window that currently has This means that if you’re in a Python Shell window after running an application, just the Python Shell window closes and not the associated Edit window.
  • Exit: Closes the current window and all associated This means that if you’re in a Python Shell window after running an application, both the Python Shell window and the associated Edit window close.

 

When you close a window, IDLE checks to ensure that you have saved any content to disk. If you haven’t saved the content, you see a dialog box asking whether you want to save it.

 

The File➪Close and File➪Exit commands affect only the current session. For example, if you open two separate Python files, you need to close each file separately because each file is opened in a separate session.