MCS-201 · Unit 9

Introduction to Python

Section 4 of 7

Block 3 · Unit 9Introduction to Python11 min · Section 4 of 7

9.4Modules, Packages, Libraries and Frameworks

Covers IGNOU MCS-201 §9.4

Python code is organised in layers, from a single file up to large frameworks. The terms are often mixed up, so this section defines each one precisely and shows how to import them.

The four terms

TermWhat it isExample
ModuleA single .py file containing definitions (functions, classes, variables) and statementsmath, or your own geometry.py
PackageA directory of modules (and sub-packages), imported with dotted namesxml, email, game.sound
LibraryAn informal term for reusable code distributed for others to use; it may be one module or many packagesNumPy, Requests, the standard library
FrameworkA library that provides the overall structure of an application and calls your code at the right momentsDjango, Flask, PyTorch’s training loop helpers

Creating and using a module

Any .py file is a module. Its name is the file name without .py.

Creating a package

A package is a folder. Placing an __init__.py file in it makes it a regular package; the file can be empty, or it can run set-up code and define __all__, the list of names that from package import * brings in. Since Python 3.3, a folder without __init__.py still works as a namespace package, but regular packages remain the normal choice for your own code.

Package layout
project/
    main.py
    game/
        __init__.py
        sound.py
        graphics.py

Key takeaways

  • Module = one .py file; package = folder of modules; library and framework are broader, informal terms.
  • import m, from m import x, and import m as alias are the three common import forms.
  • __init__.py marks a regular package and can define __all__ for import *.
  • A framework calls your code; you call a library.

Practice questions

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

Question 1 · multiple choice

What is a Python module?

Show answer

Answer: A. A single .py file containing Python definitions and statements

Every .py file is a module, named after the file. A folder of modules is a package.

Question 2 · multiple choice

After from geometry import circle_area, how is the function called?

Show answer

Answer: C. circle_area(3)

from … import copies the name into your file, so no module prefix is used.

Question 3 · multiple choice

What does this program print?

Python · quiz_alias.py
import math as m
print(m.sqrt(49) + m.floor(2.7))
Show answer

Answer: C. 9.0

m is an alias for math; sqrt(49) is the float 7.0 and floor(2.7) is 2, so the sum is the float 9.0.

Question 4 · multiple choice

Which statement is a valid import with an alias?

Show answer

Answer: D. import game.sound as snd

An alias is a single identifier written after as.

Question 5 · multiple choice

What decides which names from game import * imports when game is a package?

Show answer

Answer: B. The __all__ list in game/__init__.py, if it is defined

__all__ lists the public names (here, sub-modules); without it, only names defined in __init__.py are imported.

Question 6 · short answer

Explain, with one example each, the difference between a library and a framework.

Show worked answer

With a library, your program is in control and calls the library’s functions when it needs them; for example, your script calls pandas.read_csv("sales.csv") to load data. With a framework, the framework is in control and calls your code; for example, in Django you write a view function, and Django calls it whenever a matching web request arrives. This reversal is called inversion of control.

↑ ↓ move↵ openTab switch type