Skip to content

Interacting with Python

Interacting with Python



In This Chapter

▶ Accessing the command line

▶ Using commands to perform tasks

▶ Obtaining help about Python

▶ Ending a command-line session






ltimately, any application you create interacts with the computer and the data it contains. The focus is on data because without data, there

isn’t a good reason to have an application. Any application you use (even one as simple as Solitaire) manipulates data in some way. In fact, the acronym CRUD sums up what most applications do:


  • Create
  • Read
  • Update
  • Delete


If you remember CRUD, you’ll be able to summarize what most applications do with the data your computer contains (and some applications really are quite cruddy). However, before your application accesses the computer, you have to interact with a programming language that creates a list of tasks to perform in a language the computer understands. That’s the purpose of this chapter. You begin interacting with Python. Python takes the list of steps you want to perform on the computer’s data and changes those steps into bits the computer understands.




Understanding the importance of the README file


Many applications include a README file. The README file usually provides updated informa­ tion that didn’t make it into the documentation before the application was put into a produc­ tion status. Unfortunately, most people ignore the README file and some don’t even know it exists. As a result, people who should know something interesting about their shiny new product never find out. Python has a README. txt file in the \Python33 directory. When you open this file, you find all sorts of really interesting information:

  • How to build a copy of Python for Linux systems
  • Where to find out about new features in this version of Python
  • Where to find the latest version of the Python documentation
  • How to convert your older Python applica­ tions to work with Python 3.x
  • What you need to do to test custom Python modifications
  • How to install multiple versions of Python on the same system
  • How to access bug and issue tracking for Python
  • How to request updates to Python
  • How to find out when the next version of Python will come out

Opening and reading the README file will help you become a Python genius. People will be amazed that you really do know something interesting about Python and will ask you all sorts of questions (deferring to your wisdom). Of course, you could always just sit there, thinking that the README is just too much effort to read.




Opening the Command Line

Python offers a number of ways to interact with the underlying language. For example, you worked a bit with the Integrated DeveLopment Environment (IDLE) in Chapter 2. IDLE makes it easy to develop full-fledged applications. However, sometimes you simply want to experiment or to run an existing application. Often, using the command-line version of Python works better  in these cases because it offers better control over the Python environment through command-line switches, uses fewer resources, and relies on a mini- malistic interface so that you can focus on trying out code rather than play- ing with a GUI.



Starting Python

Depending on your platform, you might have multiple ways to start the com- mand line. Here are the methods that are commonly available:


  • Select the Python (command-line) option found in the Python 3 folder. This option starts a command-line session that uses the default settings.
  • Open a command prompt or terminal, type Python, and press Enter. Use this option when you want greater flexibility in configuring the Python environment using command-line
  • Locate the Python folder, such as C:\Python33 in Windows, and open the exe file directly. This option also opens a command-line session that uses the default settings, but you can do things like open it with increased privileges (for applications that require access to secured resources) or modify the executable file properties (to add command-line switches).


No matter how you start Python at the command line, you eventually end up with a prompt similar to the one shown in Figure 3-1. (Your screen may look slightly different from the one shown in Figure 3-1 if you rely on a platform other than Windows, you’re using IDLE instead of the command-line version of Python, your system is configured differently from mine, or you have a dif- ferent version of Python.) This prompt tells you the Python version, the host operating system, and how to obtain additional information.








Figure  3-1: The Python command prompt tells you a bit about the Python





Using the command line to your advantage

This section will seem a little complicated at first, and you won’t normally need this information when using the book. However, it’s still good informa- tion, and you’ll eventually need it. For now, you can browse the information so that you know what’s available and then come back to it later when you really do need the information.


To start Python at a command prompt, type Python and press Enter. However, that’s not all you can do. You can also provide some additional information to change how Python works:


  • Options: An option, or command-line switch, begins with a minus sign followed by one or more For example, if you want to obtain help about Python, you type Python –h and press Enter. You see additional information about how to work with Python at the command line. The options are described later in this section.
  • Filename: Providing a filename as input tells Python to load that file and run You can run any of the example applications from the download- able code by providing the name of the file containing the example as input. For example, say that you have an example named To run this example, you type Python and press Enter.
  • Arguments: An application can accept additional information as input to control how it This additional information is called an argument. Don’t worry too much about arguments right now — they appear later in the book.


Most of the options won’t make sense right now. They’re here so that you can find them later when you need them (this is the most logical place to include them in the book). Reading through them will help you gain an understanding of what’s available, but you can also skip this material until you need it later.


Python uses case-sensitive options. For example, -s is a completely different option from -S. The Python options are


  • -b: Add warnings to the output when your application uses certain Python features that include: str(bytes_instance), str(bytearray_ instance), and comparing bytes or bytearray with str().
  • bb: Add errors to the output when your application uses certain Python features that include: str(bytes_instance), str(bytearray_ instance), and comparing bytes or bytearray with str().
  • -B: Don’t write .py or .pyco files when performing a module



  • -c cmd: Use the information provided by cmd to start a This option also tells Python to stop processing the rest of the information as options (it’s treated as part of the command).
  • -d: Start the debugger (used to locate errors in your application).
  • -E: Ignore all the Python environment variables, such as PYTHONPATH, that are used to configure Python for
  • -h: Display help about the options and basic environment variables Python always exits after it performs this task without doing anything else so that you can see the help information.
  • -i: Force Python to let you inspect the code interactively after running a It forces a prompt even if stdin (the standard input device) doesn’t appear to be a terminal.
  • -m mod: Run the library module specified by mod as a This option also tells Python to stop processing the rest of the informa- tion as options (the rest of the information is treated as part of the command).
  • -O: Optimize the generated bytecode slightly (makes it run faster).
  • -OO: Perform additional optimization by removing doc-strings.
  • -q: Tell Python not to print the version and copyright messages on inter- active
  • -s: Force Python not to add the user site directory to path (a vari- able that tells Python where to find modules).
  • -S: Don’t run ‘import site’ on Using this option means that Python won’t look for paths that may contain modules it needs.
  • -u: Allow unbuffered binary input for the stdout (standard output) and

stderr (standard error) devices. The stdin device is always buffered.

  • -v: Place Python in verbose mode so that you can see all the import Using this option multiple times increases the level of verbosity.
  • -V: Display the Python version number and
  • –version: Display the Python version number and exit.
  • -W arg: Modify the warning level so that Python displays more or fewer The valid arg values are
  • action
  • message
  • category
  • module
  • lineno



  • -x: Skip the first line of a source code file, which allows the use of non- Unix forms of #!cmd.
  • -X opt: Set an implementation-specific (The documentation for your version of Python discusses these options, if there are any.)