1. Introduction: From Functions to Modules
In previous topics, functions were introduced as modular units of code designed to perform specific tasks. While functions organize small segments of logic, real-world software and artificial intelligence applications require structuring much larger portions of code.
In Python, larger code collections are organized into modules:
- Function: Packages a small segment of logic for reuse within a program.
- Module: A dedicated file containing tested, well-designed Python code (functions, variables, and classes) that can be imported and utilized across multiple programs.
One of Python’s greatest advantages over older programming languages is its massive ecosystem of pre-built modules and libraries. Especially in Artificial Intelligence (AI), Python provides extensive built-in and open-source modules that eliminate the need to write complex algorithms from scratch.
2. Common Built-In Python Modules
Python comes bundled with a standard library of pre-installed modules, including:
math — Provides comprehensive mathematical functions (such as square roots and trigonometry).
random — Generates pseudo-random numbers and random selections.
os — Facilitates direct interaction with the operating system and file system paths.
To use any module, Python provides the import keyword.
3. Method 1: Importing an Entire Module
To make an entire module and all of its functions accessible within your file, use the import keyword followed by the module name:
import math
Calling Module Functions: The Dot Operator (.)
When an entire module is imported, you access its functions by writing the module name, followed by the dot operator (.), followed by the function name:
import math
# Calculating the square root of 16
result = math.sqrt(16)
print(result)
Output:
4.0
Why the Dot Operator Matters (Namespace Protection)
- Avoiding Naming Collisions: You could write your own custom function named
sqrt() inside your script.
- If you call
sqrt(16), Python executes your locally defined function.
- If you write
math.sqrt(16), Python explicitly calls the optimized function from the imported math module.
- The return value is a floating-point number (
4.0) because roots frequently evaluate to decimal values.
4. Method 2: Importing Specific Functions (from ... import ...)
If your script only requires a specific tool rather than an entire library, importing the entire module is unnecessary. You can selectively import individual functions using the from and import keywords:
from math import sqrt
Direct Invocation Without Module Prefixes
When a function is imported individually, it enters your local namespace directly, allowing you to call it without prefixing the module name:
from math import sqrt
# Calling the function directly without 'math.'
print(sqrt(25))
Output:
5.0
Python calculates the square root of 25 and directly prints 5.0.
- Entire module:
import math then math.sqrt(25) — requires the math. prefix.
- Specific function:
from math import sqrt then sqrt(25) — direct call, no prefix.
5. Creating and Using Custom Modules
Beyond utilizing built-in libraries, Python allows developers to create their own custom modules to share code across projects.
Step 1: Create a Python File (.py)
In your project directory, create a standard Python file with a .py extension (for example, mymodule.py). Inside this file, define one or more reusable functions:
# Content saved inside mymodule.py
def greet():
print("Hello I am the modules file")
Step 2: Import the Custom Module in Your Main Script
In your Jupyter Notebook or working script located in the same directory, import the file using its filename without the .py extension:
import mymodule
# Calling the function defined inside mymodule.py
mymodule.greet()
Output:
Hello I am the modules file
How Python Resolves Module Names
When import mymodule executes:
- Python searches its built-in and globally installed system libraries to check if a module with that name exists.
- If no system library matches, Python checks the local working directory/path and loads your local
mymodule.py file.
- You can define multiple functions within a single custom module and call each using
mymodule.function_name().