Concepts / Importing modules

Importing modules

A module is a .py file containing functions and variables designed to be reused by other programs.

  • Programming

The Reuse Problem

Imagine that several programs need the same function. Without a module, you may copy that function into each program. If the function needs to change, each copied version must be found and updated. Modules solve this duplication problem by letting you write a function once and use it in many different programs.

reusable functionalityreusable functionalityreusable functionalityshared_tools.pyshared functions andvariablesProgram Auses the moduleProgram Buses the moduleProgram Cuses the module
How does one shared .py module provide reusable functions to several different programs?

A Module as a Reusable File

A module is a .py file containing functions and variables designed to be reused by other programs.

The important idea is not a special kind of Python function or variable. A module is created from regular Python code. Its distinguishing purpose is reuse: another program can import the module and use the functions and variables it contains. This gives several programs one shared source of truth for common functionality.

A module is both a file and a reuse boundary: the .py file holds shared functions and variables, while other programs import and use that shared code.

Creating the File

  1. Create a .py file with a descriptive name.
  2. Place the regular Python functions and variables that you want to reuse in that file.
  3. Have other programs import the module when they need those shared functions or variables.

Generated example: a developer could place commonly reused functions and variables in a descriptive file named shared_tools.py. A first program and a second program could both import that module instead of keeping separate copies of the same functionality.

What Importing Provides

When a program imports a module, the program can use the functions and variables designed for reuse in that module. The module remains the shared place where that functionality is maintained. If several programs use the same module, they all depend on that single source of truth rather than on separately copied versions.

importsprovides reusable functions and variablesProgramneeds shared functionalityModulefunctions and variablesProgramuses imported functionality
What happens to a module's reusable functions and variables when another Python program imports it?

Two Programs Sharing One Function

Two programs need the same function. How can their shared functionality be organized?

Choose the shared code: Identify the function that both programs need to use.

Place it in one module: Write the function in a descriptive .py file. That file is the module.

Import it in each program: Each program imports the same module instead of keeping its own copied version of the function.

Maintain one source: The shared function is maintained in the module, so the programs use one common source of functionality.

One module supplies reusable functionality to both programs, reducing code duplication.

Module or Script

File roleMain purposeTypical contents
ModuleProvide reusable code to other programsFunctions and variables designed for reuse
Regular scriptRun as a program directlyPython code organized for that program's own task

Both a module and a regular script can be Python files. The distinction emphasized here is their purpose. A module is a .py file whose functions and variables are designed to be reused by other programs. A script is treated as a program to run directly rather than primarily as a shared source of functionality.

Mistakes with Modules

  • Copying the same function into every program

    Copies create duplicated code instead of one shared source of functionality.

    Fix: Place the function in one descriptive .py module and have both programs import it.

  • Assuming a module requires a special file format

    The simplest module is created by writing regular Python functions and variables in a .py file.

    Fix: Use a descriptive .py filename and place the reusable Python code inside it.

  • Treating every .py file as a reusable module

    A module is specifically designed for reuse by other programs.

    Fix: Distinguish files by intended purpose: shared functionality for a module, or direct program execution for a script.

Practice and Summary

EASY

A team has three programs that need the same function. Describe where you would place the function, what kind of file you would create, and what each program would do to use the shared functionality.

Hints
  • Think about the single source of truth.
  • The simplest module is a descriptive .py file containing regular Python code.
  • The programs should import the shared module.
  1. Modules reduce duplication by allowing one function to be used in many programs. The simplest module is a descriptive .py file containing regular Python functions and variables. Other programs import the module to use that shared functionality. A module is intended for reuse, while a regular script is intended to run directly as a program.

Key Takeaways

  • A module is a .py file containing functions and variables designed for reuse.
  • Modules let multiple programs use one shared implementation instead of copied code.
  • The simplest module is made by placing regular Python code in a descriptively named .py file.
  • Importing lets another program use the module's reusable functions and variables.
  • A module is organized for reuse, while a regular script is organized to run directly.