Creating your own modules

18-minute lesson · module-modules-04

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Creating your own modules

18-minute lesson · module-modules-04

  • Explain that any `.py` file is a module other files can import.
  • Organize related functions and constants into one reusable module.
  • Import names from your own module the same way you import the standard library.
  • Describe the role of the `if __name__ == "__main__"` guard.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Modules / Creating Modules / More About Using Modules and Functions

Adaptation notes. Reading rewritten in our voice. The worked module is a small Iowa State "gradebook" toolkit. Because the in-browser sandbox runs the student's file as a single program, the exercise has the student build the module's functions and constants; the hidden tests import that very file as a module (via importlib) and exercise the public names — exactly what a second file would do.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.