Modules and Libraries in Python

Modules and libraries let you use code without placing all of it in one program file. You encounter them when you need mathematical tools, file-handling tools or code shared across a project. The examples here use Python. Your main task is to recognize what an import makes available and then write the matching name when you use that code.

Reusable code has an organization

A module is a unit of Python code with its own names, commonly stored in a file ending in .py. A module can provide functions, classes and other values. Importing it lets another program access those objects. You do not need to copy their definitions into the importing file.


A library is a collection of reusable code intended to support other programs. A library may contain one module or many related modules. Python's standard library includes math, which supplies mathematical functions and constants. Other libraries are distributed separately.


A package helps organize modules. For example, a project might provide a module named shop.pricing. The dots describe its organization; they do not mean that every component is a separate library.


These words describe different levels of organization. When someone says that a program uses a library, the code will usually import particular modules or names from that library.


A reporting program imports a module and accesses a function through the module name.


Match the import to the name you use

A module has a namespace. Keeping its names together lets different modules provide similarly named tools without immediately conflicting.


The governing pattern for importing a module is:


Python
import module_name
module_name.member_name


For a real example, importing math makes the name math available. You then use a dot to select a member:


Python
import math
result = math.sqrt(49)


Example: This code stores 7.0 in result. The expression math.sqrt identifies the square-root function belonging to the imported module. Importing math does not also make the bare name sqrt available.


You can also select a name directly or give an imported object an alternative name:


Import statement

Name made available directly

Matching call

import math

math

math.sqrt(49)

import math as m

m

m.sqrt(49)

from math import sqrt

sqrt

sqrt(49)

from math import sqrt as root

root

root(49)


The keyword as introduces an alias. An alias changes the name used in your program; it does not rename the original module file or change what the imported function calculates.



Keep imports predictable

Importing code and installing a library are separate actions. An import accesses code that Python can already locate. It does not download a missing library. Standard-library modules such as math normally need no separate installation.


Use module-qualified access when you want the source of a tool to remain visible. Direct imports can make frequently used names shorter, but importing the same name from another source can replace an earlier binding. Avoid wildcard imports such as from math import * in introductory programs because they make the available names harder to track.


A directly imported name is also a separate binding. If you import a numeric value from a module and later reassign that module's attribute, the earlier imported name does not automatically switch to the new value.



What to remember

  • A module supplies code and names; a library collects reusable code, and packages organize modules.
  • The import form determines the name you use afterward.
  • Importing a module usually leads to dotted access; importing a member directly makes that member's chosen name available.
  • An import uses available code; it does not install missing code.

Example: Importing Tools for Bakery Packing

Example: A bakery has 26 cookies and bags that hold at most four cookies each. You will use Python's math module to calculate two packing quantities. Its ceil function rounds up to an integer, and its floor function rounds down to an integer.


A qualified name accesses a member through another named object, such as packing.ceil. Keep track of the name each import introduces before writing a call.

Part a: Find the number of bags needed

Import the math module using the alias packing. Write a call that finds the minimum number of bags needed for all 26 cookies, and calculate its result.


First, choose the required module alias, ________, and write the import:


Python
import math as packing


The unrounded number of bag-capacity units is ________ because:


26 cookies4 cookies per bag=6.5 bags.\frac{26\ \text{cookies}}{4\ \text{cookies per bag}}=6.5\ \text{bags}.


You need a whole number of bags, with enough capacity for every cookie. Use the ceiling function through the imported module name:


Python
bags_needed = packing.ceil(26 / 4)


Rounding 6.5 upward gives bags_needed = 7. Six bags would hold only 24 cookies, so 7 bags are needed. The module alias must appear before the dot.

Part b: Find the number of completely filled bags

Import only the floor function using the alias complete. Write a call that finds how many bags can be completely filled with the same cookies, and calculate its result.


Import the selected function directly and give it the requested alias:


Python
from math import floor as complete


The same division gives 6.5 bag-capacity units. To count only completely filled bags, round down:


Python
full_bags = complete(26 / 4)


The floor of 6.5 is 6. As a check, six full bags use 24 cookies and leave ________ for the remaining bag:


26 cookies−(6)(4 cookies)=2 cookies.26\ \text{cookies}-(6)(4\ \text{cookies})=2\ \text{cookies}.


Therefore 6 bags can be completely filled. The name complete already refers to the imported function, so this call needs no module prefix.

A student is organizing reusable Python code for a school project. Match each term to the description that best identifies its role.

Match each term
A.

A unit of Python code with its own names, commonly stored in one .py file.

B.

An organizational unit that groups related modules under a shared dotted name.

C.

A collection of reusable code intended to support other programs.

D.

A dotted name that includes the object through which a member is accessed, such as math.sqrt.

Library

Module

Qualified name

Package

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An inventory helper named catalog.py contains exactly the following statement. Consider two separate runs, each starting in a fresh Python interpreter.


Python
print(__name__ == "__main__")


When you execute catalog.py directly, it prints . When another file executes import catalog, the helper prints .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A student runs the following Python code in a fresh session with no earlier imports or assignments. Which replacement for the print line correctly uses the imported module?


Python
import math as calc
print(math.sqrt(81))
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice