Modules in Python are files containing Python code. This code can define functions, classes, and variables. A module is a way to structure your Python code and make it more manageable by dividing it into smaller, self-contained units.
Creating a Module
To create a module, you simply write your Python code in a file with a .py
extension.
Explanation:
greet(name)
: This function takes a name and returns a greeting string.add(a, b)
: This function takes two numbers and returns their sum.pi
: This is a constant representing the value of π.
# mymodule.py def greet(name): return f"Hello, {name}!" def add(a, b): return a + b pi = 3.14159
Using a Module
To use a module, you need to import it into your Python script using the import
statement.
Explanation:
import mymodule
: This statement imports the modulemymodule
.mymodule.greet("Alice")
: Calls thegreet
function frommymodule
with “Alice” as an argument.mymodule.add(5, 3)
: Calls theadd
function frommymodule
with 5 and 3 as arguments.mymodule.pi
: Accesses thepi
variable frommymodule
.
# main.py import mymodule # Using functions from the module print(mymodule.greet("Alice")) # Output: Hello, Alice! print(mymodule.add(5, 3)) # Output: 8 # Accessing variables from the module print(mymodule.pi) # Output: 3.14159
Importing Specific Items
You can import specific functions, classes, or variables from a module using the from ... import ...
statement.
Explanation:
from mymodule import greet, pi
: Imports only thegreet
function andpi
variable frommymodule
.greet("Bob")
: Calls thegreet
function with “Bob” as an argument.pi
: Accesses thepi
variable directly.
# main.py from mymodule import greet, pi # Using the imported function and variable print(greet("Bob")) # Output: Hello, Bob! print(pi) # Output: 3.14159
Using Aliases
You can assign an alias to a module or an item within a module to make it easier to reference.
Explanation:
import mymodule as mm
: Importsmymodule
and gives it the aliasmm
.from mymodule import add as sum_up
: Imports theadd
function frommymodule
and gives it the aliassum_up
.mm.greet("Carol")
: Calls thegreet
function frommymodule
using the aliasmm
.sum_up(10, 5)
: Calls theadd
function using the aliassum_up
.
# main.py import mymodule as mm from mymodule import add as sum_up # Using the module and function with aliases print(mm.greet("Carol")) # Output: Hello, Carol! print(sum_up(10, 5)) # Output: 15
Exploring the sys
Module
The sys
module provides access to some variables and functions used or maintained by the Python interpreter.
Explanation:
import sys
: Imports the built-insys
module.sys.version
: Retrieves the Python version.sys.platform
: Retrieves the platform on which Python is running.
import sys print("Python version:", sys.version) print("Platform:", sys.platform)
Python Modules Example Code
Explanation:
- mymodule.py: Defines the module with a greeting function, an addition function, and a constant.
- main.py: Imports the module and demonstrates using its functions and variables.
Step 1: Create a module named mymodule.py
.
# mymodule.py def greet(name): return f"Hello, {name}!" def add(a, b): return a + b pi = 3.14159
Step 2: Create a script named main.py
that imports and uses the module.
# main.py import mymodule # Using functions from the module print(mymodule.greet("Alice")) # Output: Hello, Alice! print(mymodule.add(5, 3)) # Output: 8 # Accessing variables from the module print(mymodule.pi) # Output: 3.14159