StudyToCert

All certifications / PCAP / Lessons

PCAP – Certified Associate Python Programmer PCAP-31-03 · Domain 1: Modules and Packages

Import variants: import, import as, from … import, from … import *, and what each puts in the namespace

▶ Watch the overview video

Last reviewed September 25, 2026 · Leer en español

A module is simply a file of Python code, and importing it lets you reuse what it defines. The PCAP exam cares less about the fact that you can import and more about exactly which names become available afterwards. Every module and script has its own namespace: a table that maps names to objects. Each import form adds different entries to the importing namespace, and many exam questions are really asking whether a particular name exists at a particular moment.

The plain form import math adds exactly one name, math, which refers to the module object. Everything inside it must be qualified: math.pi, math.sqrt(2). Writing sqrt(2) on its own raises NameError because sqrt was never placed in your namespace. The aliased form import math as m adds only m; the name math is not defined, so math.pi fails while m.pi works. Aliases are handy for long module names and are common with modules such as import numpy as np in the wider ecosystem.

The form from math import sqrt, pi works the other way round. It adds sqrt and pi directly, so you call sqrt(2) without a prefix, but the name math is not defined. You can alias individual names too: from math import sqrt as root. Because the names land directly in your namespace, they can collide with your own: if you later write pi = 3, you have replaced the imported value, and if you define your own sqrt and then import one, the import wins because it happened later. The last binding always wins.

The star form from math import * copies every public name from the module into your namespace. If the module defines a list called __all__, only the names in that list are imported; otherwise every name that does not start with an underscore is imported. Star imports are discouraged in real code because they hide where a name came from and can silently overwrite your own names, but the exam expects you to know the underscore and __all__ rules.

import math as m
print(m.floor(2.7))      # 2
# print(math.pi)         # NameError: math is not defined

from math import pi
print(pi)                # 3.141592653589793
pi = 3
print(pi)                # 3 - your assignment replaced it

Two more facts round this out. A module's code runs only on its first import in a process; later imports reuse the already-loaded module object stored in sys.modules. And every import form executes the whole module file the first time, even from mod import one_name, because Python has to run the file to create the name.

Key terms

Namespace
A mapping from names to objects; each module, function call and class has its own.
Alias
An alternative name given with as, for example import math as m, which binds only m.
__all__
A list of strings in a module naming what from module import * should import.
Qualified name
A name written with its module prefix, such as math.sqrt.
Real-world example

A data script starts with from statistics import mean and later defines its own function called mean to handle missing values. Because the def comes after the import, every later call uses the local version, and the original import is shadowed without any warning.

Exam tip: Ask which names each line adds. import m as x defines only x, never m; from m import f defines only f, never m. Many answer options that look fine fail with NameError for exactly this reason.

Check yourself

After import random as r, does print(random.random()) work?

No. Only the alias r is defined, so random raises NameError; you must write r.random().

Which names does from mod import * skip when mod has no __all__?

Names beginning with an underscore, such as _helper; all other top-level names are imported.

If a module is imported twice in the same program, how many times does its top-level code run?

Once. The second import finds the module already in sys.modules and reuses it.

Study PCAP for free
A week-by-week plan with every lesson, quizzes, checkpoint tests, a practice exam and hands-on labs.
Open the PCAP study plan