Lesson 37 of 55
9 mins readPython Modules, Namespaces & Exports
In Plain English
Modules encapsulate code into independent global namespaces. Learn to define modules, export public APIs with export, and consume functionality with using and import.
Deep Dive: How It Works
module Definition: module Geometry export circle_area; circle_area(r) = π*r^2 end.
using vs import: using brings exported symbols directly into the current namespace; import requires explicit module prefixing or allows adding new methods to imported functions.
Submodules: Modules can be nested hierarchically inside other modules.
Core Rules to Remember

Namespace Isolation: Prevent global variable collisions across large software architectures.

Explicit export API: Control exactly which functions and types are exposed to callers.
Live Interactive Example
Hit Run Code to see it liveDefining and Consuming a Custom Module
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output Console
Click "Run Code" to view the rendered output.
How it works: using .StatisticsHelper brought mean_val and variance_val into scope.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Define MathModule and Export Double
Define a module `MathModule` that exports `double_val(x) = 2x`.
Load it with `using .MathModule`.
Print `"Double 21: $(double_val(21))"`.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.