Concepts / Importing modules

Importing modules

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.

  • 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.

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

A Module as a Reusable File

Definition

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.

Key point

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. 1
    Create a .py file with a descriptive name.
  2. 2
    Place the regular Python functions and variables that you want to reuse in that file.
  3. 3
    Have other programs import the module when they need those shared functions or variables.
In the real world

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.

Programneeds shared functionalityModulefunctions and variablesProgramuses imported functionalityimportsprovides reusable functions and variables
What happens to a module's reusable functions and variables when another Python program imports it?
Worked example

Two Programs Sharing One Function

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

  1. 1
    Choose the shared code

    Identify the function that both programs need to use.

  2. 2
    Place it in one module

    Write the function in a descriptive .py file. That file is the module.

  3. 3
    Import it in each program

    Each program imports the same module instead of keeping its own copied version of the function.

  4. 4
    Maintain one source

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

Result

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

The key distinction is the intended role of the file: reuse by other programs versus direct execution as a program.

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

  • Common mistake

    Copying the same function into every program

    What it looks like

    Program A and Program B each contain their own copy of a shared function.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming a module requires a special file format

    What it looks like

    Looking for a separate module file type instead of using a regular .py file.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Treating every .py file as a reusable module

    What it looks like

    Calling a file a module even when it is organized only to run one program directly.

    Why it fails

    A module is specifically designed for reuse by other programs.

    Do this instead

    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. 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.

At a glance

  • 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.

Related concepts

More concepts starting with I