11 Python programs

 

This chapter covers

  • Creating a very basic program
  • Making a program directly executable on Linux/ UNIX
  • Writing programs on macOS
  • Selecting execution options in Windows
  • Combining programs and modules
  • Distributing Python applications

Up until now, you’ve been using the Python interpreter mainly in interactive mode in Colaboratory. For production use, you may need to create Python programs or scripts. Several of the sections in this chapter focus on command-line programs. If you come from a Linux/UNIX background, you may be familiar with scripts that can be started from a command line and given arguments and options that can be used to pass in information and possibly redirect their input and output. If you’re from a Windows or Mac background, these things may be new to you, and you may be more inclined to question their value. While you certainly can use Python from the command line in any of those environments, it becomes a bit trickier if you are using an Android or iOS device. Since we have been using Colaboratory as our reference environment, we’ll discuss how to run command-line scripts from within a notebook.

11.1 Creating a very basic program

11.1.1 Starting a script from a command line

11.1.2 Command-line arguments

11.1.3 Executing code only as main script

11.1.4 Redirecting the input and output of a script

11.1.5 The argparse module

11.1.6 Using the fileinput module

11.2 Running scripts in different operating systems

11.2.1 Making a script directly executable on UNIX

11.2.2 Scripts on macOS

11.2.3 Script execution options in Windows

11.3 Programs and modules

11.4 Distributing Python applications

11.4.1 Wheels packages

11.4.2 zipapp and pex

11.4.3 py2exe and py2app

11.4.4 Creating executable programs with freeze

11.5 Creating a program

11.5.1 Solving the problem with AI-generated code

11.5.2 Solutions and discussion

Summary