Python 5.1Code walkthrough8 parts

Modules and Packages

Split code across files and import it; use other people's packages.

In this lesson8 parts

  1. 01Our functions, in a file
  2. 02What import does
  3. 03The standard library
  4. 04Installed packages
  5. 05`__name__` and the main guard
  6. 06Packages
  7. 07Where imports mislead
  8. 08Recap, your challenge, and next

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

__name__
A module's name; "__main__" for the file being run. if __name__ == "__main__": guards the lines that should run only when the file is run.
import
Running a module once and binding a name to it.
module
A Python file that other code imports.
package
A folder of modules, imported with dotted names: review_stats.text_tools.
standard library
The modules that come with Python: math, pathlib, csv, json, importlib.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 1 · Modules and Packages.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.