liveBook · Manning

Chapter 11: Python programs

What you will learn in chapter 11 of The Quick Python Book, Fourth Edition

This introduction explains why command-line Python scripts are worth learning after interactive work in Colaboratory, especially for processing many files.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why command-line Python scripts are worth learning after interactive work in Colaboratory, especially for processing many files.

Key terms: interactive mode, Python programs or scripts, command line, arguments and options, redirect their input and output, command-line techniques, GUI environment, large numbers of files

11.1 Creating a very basic program

This section introduces a basic Python script structure: define a controlling function and then call it.

Key terms: program, script, main, controlling function

11.1.1 Starting a script from a command line

This subsection explains how to start a saved Python script from a command line in Colaboratory, Linux/UNIX, macOS, or Windows.

Key terms: Colaboratory, ! prefix, host environment, script file, path, terminal, Command Prompt, PowerShell, cd

11.1.2 Command-line arguments

This section explains how a script receives command-line arguments through sys.argv and how to interpret the values it contains.

Key terms: sys.argv, sys module, command-line arguments, list of strings

11.1.3 Executing code only as main script

This section explains how to run a script’s main code only when the file is executed directly, while keeping the file safe to import as a module.

Key terms: __name__, __main__, main, module

11.1.4 Redirecting the input and output of a script

This section shows how command-line redirection and pipes let a Python script read, transform, and pass along streams of text.

Key terms: standard input, standard output, sys.stdin, sys.stdout, print, <, input function, end-of-file indicator, Ctrl-D, >

11.1.5 The argparse module

This section shows how to use argparse to define command-line arguments and options, then access the parsed values.

Key terms: argparse, ArgumentParser, positional arguments, optional arguments, indent, int, -f, --file, quiet, action="store_false"

11.1.6 Using the fileinput module

This section shows how fileinput lets a script process lines from multiple files or standard input as one stream, and introduces functions for tracking and controlling that stream.

Key terms: fileinput module, sys.argv, standard input, lineno, filelineno, filename, isfirstline, isstdin, nextfile, fileinput.input

11.2 Running scripts in different operating systems

11.2.1 Making a script directly executable on UNIX

Explains how to make a Python script directly executable on UNIX-like systems and notes how command-line redirection works there.

Key terms: chmod, path, directly executable, <, >, redirected

11.2.2 Scripts on macOS

This section explains how to run Python scripts on macOS from a terminal or Finder and points to resources for Mac-specific scripting.

Key terms: terminal window, Finder, Python Launcher, administrative scripts, Apple’s Open Scripting Architecture, Python

11.2.3 Script execution options in Windows

This section explains ways to run Python scripts on Windows, why a command window is especially flexible, and what to check if Python does not start.

Key terms: command prompt, PowerShell, input and output redirection, python3, command path, Python executable, PATH environment variable, Linux subsystem for Windows, .py, magic extension

11.3 Programs and modules

This section shows how a controlling function can make a growing script usable both from the command line and as an importable module, with an optional regression-test mode.

Key terms: controlling function, scripts, modules, scripting plumbing, if __name__ == "__main__":, __name__, __main__, main, command-line arguments, num2words

11.4 Distributing Python applications

This section explains why simply sharing source files or bytecode is an imperfect way to distribute Python applications.

Key terms: source files, zip or tar file, bytecode, .pyc files

11.4.1 Wheels packages

Introduces wheels as the standard package format for distributing Python code and explains their intended benefits.

Key terms: wheels, modules, applications, dependencies

11.4.2 zipapp and pex

This section explains how executable zip archives package multifile Python applications and compares zipapp with pex.

Key terms: __main__.py, entry point, sys.path, shebang line, executable zip file, zipapp, pex, standard library, pip

11.4.3 py2exe and py2app

This section identifies tools for creating standalone Python executables and explains their main trade-off.

Key terms: py2exe, py2app, standalone executables, native Python applications

11.4.4 Creating executable programs with freeze

This section explains how freeze creates a standalone executable from a Python program and why that distribution route may not be worth the effort.

Key terms: freeze, C files, C compiler, frozen application, Python source distribution, standalone application

11.5 Creating a program

This section sets out an exercise to refactor a Python version of wc so command-line options control which counts it displays.

Key terms: wc, -l, -w, -c

11.5.1 Solving the problem with AI-generated code

This subsection explains how to give an AI chatbot the existing code and requirements for refactoring a word-count program.

Key terms: Colaboratory, Copilot, Unix wc utility, full chat interface

11.5.2 Solutions and discussion

11.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 10: Modules and scoping rules Chapter 12: Using the filesystem →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.