Welcome to Lesson 9! In this lesson, we'll learn how to organize code into modules and packages in Python. Modules and packages help break down large codebases into smaller, manageable units and facilitate code reuse.
A module in Python is a file containing Python definitions and statements. The file name is the module name with the .py extension. You can access the functions, classes, and variables defined in a module using the import statement.
Example 1: Creating and Importing a Module
- Create a file named
my_module.pywith the following content:
# my_module.py
def greet(name):
print(f"Hello, {name}!")
def add(a, b):
return a + b- In another file, you can import and use the functions defined in
my_module.py:
# main.py
import my_module
my_module.greet("John") # Output: Hello, John!
result = my_module.add(2, 3)
print(result) # Output: 5A package in Python is a collection of modules organized in directories. A directory containing an empty file named __init__.py is considered a package. The __init__.py file can contain initialization code for the package.
Example 2: Creating and Importing a Package
- Create a directory named
mypackage. - Inside the
mypackagedirectory, create two files:__init__.py(can be empty)my_module.pywith the following content:
# my_module.py
def greet(name):
print(f"Hello, {name}!")
def add(a, b):
return a + b- In another file, you can import and use the functions defined in
my_module.pywithin themypackagepackage:
# main.py
from mypackage import my_module
my_module.greet("Alice") # Output: Hello, Alice!
result = my_module.add(4, 6)
print(result) # Output: 10You can also import specific functions or variables from a module or package using the from keyword.
Example 3: Importing Specific Functions from a Module
# main.py
from my_module import greet
greet("Bob") # Output: Hello, Bob!Example 4: Importing Specific Functions from a Package
# main.py
from mypackage.my_module import add
result = add(8, 12)
print(result) # Output: 20- Modules in Python are similar to C#'s source files, but C# does not require an explicit import statement for files within the same project.
- Packages in Python are like C#'s namespaces but organized as directories containing
__init__.pyfiles.
Now that you've learned about modules and packages in Python, you're ready to move on to Lesson 10, where we'll explore object-oriented programming (OOP) in Python and compare it to C#'s class-based approach.
- Python Modules and Packages: https://docs.python.org/3/tutorial/modules.html
- Organize the functions from the previous practice projects (area of rectangle and word lengths) into separate modules within a package. Import and use these functions in a new main script.