Skip to content

Closing the Command Line

Closing the Command Line

Eventually, you want to leave Python. Yes, it’s hard to believe, but people have other things to do besides playing with Python all day long. You have two standard methods for leaving Python and a whole bunch of nonstandard methods. Generally, you want to use one of the standard methods to ensure that Python behaves as you expect it to, but the nonstandard methods work just fine when you simply want to play around with Python and not perform any productive work. The two standard methods are

 

  • quit()
  • exit()

 

Either of these methods will close the interactive version of Python. The shell

(the Python program) is designed to allow either command.

 

Both of these commands can accept an optional argument. For example, you can type quit(5) or exit(5) and press Enter to exit the shell. The numeric argument sets the command prompt’s ERRORLEVEL environment variable, which you can then intercept at the command line or as part of a batch file.

Standard practice is to simply use quit() or exit() when nothing has gone wrong with the application. To see this way of exiting at work, you must

 

  1. Open a command prompt or

You see a prompt.

  1. Type Python and press Enter to start

You see the Python prompt.

 

 

  1. Type quit(5) and press Enter.

You see the prompt again.

  1. Type echo %ERRORLEVEL% and press Enter.

You see the error code, as shown in Figure 3-11. When working with plat- forms other than Windows, you may need to type something other than echo %ERRORLEVEL%. For example, when working with a bash script, you  type  echo $ instead.

 

 

 

 

 

 

 

 

Figure 3-11: Add an error code when needed to tell others the Python exit status.

 

 

One of the most common nonstandard exit methods is to simply click the command prompt’s or terminal’s Close button. Using this approach means that your application may not have time to perform any required cleanup, which can result in odd behaviors. It’s always better to close Python using an expected approach if you’ve been doing anything more than simply browsing.

 

You also have access to a number of other commands for closing the command prompt when needed. In most cases, you won’t need these special commands, so you can skip the rest of this section if desired.

 

When you use quit() or exit(), Python performs a number of tasks to ensure that everything is neat and tidy before the session ends. If you sus- pect that a session might not end properly anyway, you can always rely on one of these two commands to close the command prompt:

 

  • exit()
  • _exit()

 

 

Both of these commands are used in emergency situations only. The first, sys.exit(), provides special error-handling features that you discover in Chapter 9. The second, os._exit(), exits Python without performing any of the usual cleanup tasks. In both cases, you must import the required module, either sys or os, before you can use the associated command. Consequently, to use the sys.exit() command, you actually use this code:

 

 

You must provide an error code when using os._exit() because this command is used only when an extreme error has occurred. The call to this command will fail if you don’t provide an error code. To use the os._ exit() command, you actually use this code (where the error code is 5):

 

 

Chapter 10 discusses importing modules in detail. For now, just know that these two commands are for special uses only and you won’t normally use them in an application.