Python Imports and sys.path
PCAP objective 1.1 is import and use modules and packages, and it is the first subject in either syllabus that PCEP does not mention. There are five import forms and each binds different names - which is the whole examinable content, because from math import pi gives you pi and specifically does not give you math. Beyond that: the two import failures raise different exceptions, and a module body runs exactly once no matter how many times you import it.
Modules and Packages Guide 4 of 25 Intermediate
- OSUbuntu 26.04 LTS
- Python3.14.4
- pip25.1.1
- TimeAbout 16 min
ModuleNotFoundError was added in Python 3.6 as a subclass of ImportError; older material says ImportError for a missing module and is not wrong, just less specific. Everything else here is unchanged since Python 3.0.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for the import objective, the largest single block on PCAP. This matters because the five import forms bind different names - and from x import y does not give you x, which is what this objective asks most.
- using the five import forms, and seeing what each one binds
- finding what
from ... importspecifically does not give you - meeting the two ways an import fails
- reading
sys.path, and learning that not every module is a file - seeing a module body run once however often you import it, and what
__name__tells a file
Before you start
- guide 4 - the
__pycache__behaviour comes from there. - guide 23 -
ModuleNotFoundErroris anImportError.
-
The five import forms
-
What from ... import specifically does not give you
-
The two ways an import fails
-
Where Python looks
-
Not every module is a file
-
What a module offers
-
A module body runs once, however often you import it
-
__name__ tells a file how it was started
-
What the exam does with this objective