MCS-201 · Unit 12

Modules and Packages

Section 1 of 6

Block 3 · Unit 12Modules and Packages10 min · Section 1 of 6

12.1Creating and Using Modules

Covers IGNOU MCS-201 §12.2

A module lets you write functions once and use them in any number of programs. You have already imported built-in modules such as os and shutil; here you create your own.

What a module is

Any .py file is a module. Grouping related functions, classes and variables in one module makes them reusable, avoids copying code between programs, and gives each module its own namespace: names inside series.py do not clash with the same names in another file, because they are reached as series.name.

The three ways to import

FormExampleHow names are usedBest for
Whole moduleimport mathmath.sqrt(16), math.piMost cases; clear where names come from
Whole module with aliasimport statistics as stst.mean(data)Long module names
Selected namesfrom math import pi, sqrtpi, sqrt(16) directly; math.pi is a NameErrorA few frequently used names
All public namesfrom math import *sqrt(16), log(2) directlyInteractive experiments only

from module import * imports every public name (names not starting with _), or exactly the names listed in the module’s __all__ if it defines one. It can silently overwrite your own names, so PEP 8 advises against it in programs.

What happens on import

  1. Python finds the file using the module search path (next section).
  2. It compiles the module to bytecode and, for later runs, caches the result as a .pyc file in a __pycache__ folder, so the module loads faster next time (the code itself runs at the same speed).
  3. It runs the module’s top-level code from top to bottom once, creating its functions and variables.
  4. It binds the module name in the importing file. A second import of the same module in the same program reuses the loaded module without running it again.

Key takeaways

  • A module is a .py file; its name is the file name.
  • import m, import m as x, from m import a, b, from m import *.
  • Each module has its own namespace.
  • A module runs once per program; bytecode is cached in __pycache__.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the module name of a file called bank_utils.py?

Show answer

Answer: B. bank_utils

The module name is the file name without the .py extension.

Question 2 · multiple choice

After from math import sqrt, which call works?

Show answer

Answer: C. sqrt(25)

Only the name sqrt was imported, so it is used directly; the name math is not defined.

Question 3 · multiple choice

What does this program print?

Python · quiz_import.py
import math as m
from math import floor
print(m.ceil(2.1) + floor(2.9))
Show answer

Answer: B. 5

ceil(2.1) is 3 and floor(2.9) is 2; 3 + 2 = 5.

Question 4 · multiple choice

What is stored in the __pycache__ folder?

Show answer

Answer: C. Compiled bytecode (.pyc) of imported modules

Caching bytecode lets Python skip recompiling unchanged modules, so they load faster.

Question 5 · multiple choice

A module greet.py contains print("loading") at its top level. What does a program that runs import greet twice print?

Show answer

Answer: A. loading once

A module’s top-level code runs only on its first import; later imports reuse the loaded module.

Question 6 · short answer

Explain why from module import * is discouraged in programs, with an example.

Show worked answer

It copies every public name of the module into your namespace without listing them, so a reader cannot see where a name came from, and names can be silently overwritten. For example, after def pow(a, b): ... in your file, a later from math import * replaces your pow with math.pow, which always returns a float. Writing import math and using math.pow, or importing the needed names explicitly, avoids both problems.

↑ ↓ move↵ openTab switch type