MCS-201 · Unit 12

Modules and Packages

Section 4 of 6

Block 3 · Unit 12Modules and Packages9 min · Section 4 of 6

12.4Creating and Importing Packages

Covers IGNOU MCS-201 §12.5

When a project has many modules, a flat folder of files becomes messy. A package groups related modules into a folder, so that names such as area.circle show the structure of the project.

What a package is

ModulePackage
What it isOne .py fileA folder of modules and sub-packages
Name comes fromThe file nameThe folder name
Special fileNone__init__.py (marks a regular package)
Import exampleimport circleimport area.circle

init.py and all

  • __init__.py runs automatically the first time the package (or anything inside it) is imported. It can be empty, or it can set up variables, import commonly used names, and define __all__.
  • __all__ is a list of names; from package import * imports exactly those names (for example the sub-modules listed). Without __all__, from package import * brings in only the names defined in __init__.py itself, not the sub-modules.
Package layout
project/
    main.py
    area/
        __init__.py
        circle.py
        square.py
        rectangle.py

Sub-packages and relative imports

A package can contain sub-packages (folders inside it), imported with longer dotted names such as shapes.solid.cube. Inside a package, modules can import their siblings with relative imports: from . import square (same package) or from .. import utils (parent package).

Key takeaways

  • A package is a folder of modules; __init__.py marks a regular package.
  • Import with import pkg.mod, from pkg import mod, or from pkg.mod import name.
  • __all__ in __init__.py controls from pkg import *.
  • Sub-packages and relative imports (from . import x) organise larger projects.

Practice questions

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

Question 1 · multiple choice

What does __init__.py do?

Show answer

Answer: B. It marks a folder as a regular package and runs when the package is first imported

It is the package’s initialisation module; it may be empty.

Question 2 · multiple choice

How do you call abc() defined in pack/first.py after import pack.first?

Show answer

Answer: C. pack.first.abc()

import pack.first binds the name pack, so the full dotted path is needed.

Question 3 · multiple choice

pack/__init__.py contains __all__ = ["first"], and the package has modules first.py and second.py. What does this program print?

Python · quiz_all.py
from pack import *
print("first" in dir(), "second" in dir())
Show answer

Answer: D. True False

Only the names listed in __all__ are imported by *, so first is defined and second is not.

Question 4 · multiple choice

Which import makes the name square refer to the module area/square.py?

Show answer

Answer: B. from area import square

from package import module binds the module’s name directly.

Question 5 · multiple choice

What does from . import square inside area/circle.py mean?

Show answer

Answer: C. Import the module square from the same package as circle

A single dot in a relative import means “the current package”.

Question 6 · short answer

Explain the difference between a module and a package, and show the import statement needed to call area() from area/circle.py as circle.area(5).

Show worked answer

A module is a single .py file containing definitions; a package is a folder that groups related modules (and sub-packages), normally with an __init__.py file, and its modules are reached with dotted names. To call the function as circle.area(5), write from area import circle, which binds the name circle to the module area/circle.py.

↑ ↓ move↵ openTab switch type