12DESIGNING AND DEPLOYING COMMAND LINE PROGRAMS

Users are accustomed to interacting with the computer through a graphical user interface (GUI), but experienced programmers are much more effective when working in a command line terminal. Once you’ve mastered the command line, you’ll know how to create minimal but efficient programs with simple text-based interfaces.

A simple drawing of a light bulb. LEARNING OBJECTIVES

  • Know how to set up your Python programs to easily run them outside the code editor.
  • Understand command line interfaces, including how programs present information as output and accept user input.
  • Become familiar and comfortable with software jargon.
  • Be able to create virtual environments for your programming projects and avoid package version conflicts.
  • Master the use of PyMsgBox to create dialog boxes for a lightweight GUI.
  • Know how to compile your Python programs with PyInstaller so that you can run them on computers that don’t have Python installed.

A grey circle with a white question mark at the center Practice Questions

These questions test your understanding of the terminal in Windows, macOS, and Linux and your ability to run Python programs from them.

A Program by Any Other Name

Programming uses many terms that mean “a program” or some slight variation of the term. But there are subtle differences between what these names mean. Answer the following questions about different jargon terms.

  1. 1. What’s the difference between a program and a command?

  2. 2. What’s the difference between a command and an application?

  3. 3. What’s an interactive command?

  4. 4. Do the terms script, command, application, and web app all refer to types of programs?

Using the Terminal

A command line interface doesn’t have the icons, buttons, and graphics of a GUI, but it’s an effective way to use a computer once you’ve learned several of its commands. Answer the following questions about the command line terminal.

  1. 5. What are the names of the terminal applications on Windows, macOS, and Linux?

  2. 6. What does the tilde character (~) represent in the terminal?

  3. 7. What is the filename of the Python interpreter called on Windows?

  4. 8. What does the pwd command do?

  5. 9. How can you find out what the current working directory is on Windows?

  6. 10. What commands display the contents of the current working directory on Windows, macOS, and Linux?

  7. 11. How do you display all the executable files in the current working directory on Windows?

  8. 12. How do you display all the executable files in the current working directory on macOS and Linux?

  9. 13. What command do you enter if you want to open example.txt with the default text editor app on Windows?

  10. 14. What command do you enter if you want to open example.txt with the default text editor app on macOS?

  11. 15. Say you have a program named eggs or eggs.exe in the current working directory. What command do you enter if you want to run this program on Windows, macOS, and Linux?

  12. 16. If the eggs program isn’t in the current working directory, what happens when you run eggs in the terminal?

  13. 17. How can you show the contents of the PATH environment variable on Windows, macOS, and Linux?

  14. 18. What character separates the folder names in the PATH environment variable on Windows, macOS, and Linux?

  15. 19. If C:\Users\al\Scripts is the current working directory and the only folder name in the PATH environment variable, would entering spam.exe into the terminal run the program at C:\Users\al\Scripts\subfolder\spam.exe?

  16. 20. What file do you edit on macOS to edit the PATH environment variable?

  17. 21. What file do you edit on Linux to edit the PATH environment variable?

  18. 22. What commands (on Windows and on macOS/Linux) would tell you the folder location of a program named abcd if you entered abcd in the terminal?

Virtual Environments

Virtual environments are separate installations of Python that have their own sets of installed third-party packages. In general, each Python application you create needs its own virtual environment. This prevents Python programs that require different versions of packages from conflicting with each other.

  1. 23. Can Python have multiple versions of the same package installed at the same time?

  2. 24. What built-in Python module creates virtual environments?

  3. 25. What is the name commonly used for virtual environment folders?

  4. 26. Once you’ve activated a virtual environment, what command can you run to make sure that running python3 or python accesses the virtual environment’s Python interpreter and not the system’s Python interpreter?

  5. 27. What command shows you all of the third-party packages currently installed?

Installing Python Packages with pip

While Python’s standard library comes with modules such as sys, random, and os, there are also hundreds of thousands of third-party packages you can find on the Python Package Index (PyPI) at https://pypi.org. In Python, a package is a collection of Python code made available on PyPI, and a module is an individual .py file containing Python code. You install packages from PyPI that contain modules, and you import modules with an import statement.

  1. 28. What program on Windows, macOS, and Linux installs third-party Python packages?

  2. 29. Where are third-party Python packages downloaded from?

  3. 30. What does the command pip install automateboringstuff3 do? What is automateboringstuff3?

Self-Aware Python Programs

Several built-in variables can give your Python program useful information about itself, the operating system it’s on, and the Python interpreter running it. Answer the following questions about Python functions that hold information about the Python program and interpreter.

  1. 31. What is in the __file__ variable?

  2. 32. What happens if you enter __file__ into the interactive shell?

  3. 33. What variable holds the filepath of the Python interpreter program?

  4. 34. What is the data type of sys.version?

  5. 35. What is the data type of sys.version_info.major and sys.version_info.minor?

  6. 36. Write an if statement that checks whether the Python program is being run by version 3 or later of the Python interpreter.

  7. 37. What does the sys.platform variable contain when the Python program is run on Windows, macOS, and Linux?

  8. 38. What exception is raised if you try to import a module that isn’t installed?

Text-Based Program Design

Even when limited to text, software applications can still provide a user interface similar to modern GUIs. These kinds of applications are called text-based user interface (TUI) applications, and they’re simpler to develop than GUI applications. Answer the following questions about making programs that run in the command line terminal.

  1. 39. Why should commands have short names when variables should have long, descriptive names?

  2. 40. If python3 yourScript.py download confirm is run from the terminal, what does sys.argv contain?

  3. 41. If python3 yourScript.py download_confirm is run from the terminal, what does sys.argv contain?

  4. 42. Does the order of command line arguments matter?

  5. 43. What pyperclip function returns the text that is currently on the clipboard?

  6. 44. What pyperclip function puts text on the clipboard?

  7. 45. What is the command to clear the terminal window of text on macOS and Linux?

  8. 46. What is the command to clear the terminal window of text on Windows?

  9. 47. Write the code to play the audio in a file named hello.mp3 using the playsound module.

  10. 48. What does it mean to say a function call blocks until it’s finished?

  11. 49. What do the terms quiet mode and verbose mode mean for commands?

Pop-Up Message Boxes with PyMsgBox

You can add small GUI message boxes to your program with the third-party PyMsgBox package. PyMsgBox lets you create dialogs using Tkinter, which comes with Python on Windows and macOS. On Ubuntu Linux, you must first install Tkinter by running sudo apt install python3-tk in the terminal. The PyMsgBox functions follow the names of message box functions in JavaScript: alert(), confirm(), prompt(), and password().

  1. 50. Do the dialog boxes that PyMsgBox create appear in the terminal window?

  2. 51. What two PyMsgBox functions allow the user to enter text into a dialog box?

  3. 52. What PyMsgBox function displays a text message in a dialog box to the user?

  4. 53. What PyMsgBox function presents the user with OK and Cancel buttons?

  5. 54. Can you create an entire program that uses PyMsgBox functions instead of print() and input()?

Deploying Python Programs

You can deploy your Python program so that you can run it in as few keystrokes as possible. First, you’ll need to add your program’s folder to the PATH environment variable. Answer the following questions about setting up your Python programs.

  1. 55. What operating system uses batch files?

  2. 56. What does the Windows pause command do?

  3. 57. What operating system uses .command files?

  4. 58. What does the chmod u+x yourScript command do?

  5. 59. Should the batch file, command file, or shell script you create to run your program automatically activate a virtual environment before running the Python interpreter?

Compiling Python Programs with PyInstaller

It’s possible to create executable programs from Python code with the PyInstaller package, which generates executable programs you can run from the command line. PyInstaller doesn’t compile Python programs into machine code, per se; rather, it creates an executable program that contains a copy of the Python interpreter and your script. The benefit of compiling your Python program is that you can share your program with others who don’t have Python installed.

  1. 60. Are Python programs mostly compiled, or mostly run by interpreters?

  2. 61. What is the benefit of compiling a Python program?

  3. 62. What terminal command would run PyInstaller to compile a program named yourScript.py?

  4. 63. PyInstaller creates two folders named build and dist when it compiles a program. Which folder contains the compiled program?

  5. 64. Can you run PyInstaller on one operating system to produce compiled programs for another operating system?

  6. 65. How large are the smallest compiled Python programs: several kilobytes, several megabytes, or several gigabytes?

A simple drawing of a sharpened pencil. Practice Projects

The following projects will give you a chance to practice designing and deploying programs.

Guess the Number with PyMsgBox

Create a dialog-based GUI for the Guess the Number game from Chapter 3 of Automate the Boring Stuff with Python. Copy the source code from the book (which you can find in the downloadable content at https://nostarch.com/automate-boring-stuff-python-3rd-edition) and replace the print() and input() calls with calls to pymsgbox.alert() and pymsgbox.prompt(). When you run this program, the terminal window won’t show any text; instead, a series of dialog boxes will handle displaying output and accepting keyboard input.

Save this program in a file named msgBoxGuess.py.

Timer with PyMsgBox

Create a dialog-based GUI for a simple timer program. Instead of making print() and input() calls, your program should call pymsgbox.alert() and pymsgbox.prompt(). Have the program ask the user how many seconds the program should pause for. Then, after that amount of time has passed, display an alert message box that says, “Time’s up!”

Save this program in a file named msgBoxTimer.py.

Compiling the Timer and Guess the Number Programs

Use the PyInstaller package to create an executable program for the msgBoxGuess.py and msgBoxTimer.py programs from the previous two practice projects. You should be able to run the resulting executables on other computers without having Python installed. Record the terminal command you used to instruct PyInstaller to generate these programs as a single file.