Module 3 Lesson 8: Creating Custom Modules
Take control of your project structure. Learn how to split your code into multiple files and build your own reusable Python libraries.
Module 3 Lesson 8: Creating Custom Modules
In the previous lesson, we learned how to import modules like math and random. But you don't have to rely on Python's built-in tools. You can turn any Python file you write into a module that can be used by other parts of your program! This is the secret to building massive, professional applications.
Lesson Overview
In this lesson, we will cover:
- The Folder Structure: How to organize your files.
- Creating a Module: Saving functions in a separate file.
- Importing Your File: The path to modular code.
- The
if __name__ == "__main__":block: Protecting your module from accidental execution.
1. Creating Your First Module
Let's say you have a bunch of math helper functions. Instead of copying them into every new project, you can save them in a file named my_helpers.py.
File: my_helpers.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
2. Using Your Module
Now, create a second file in the same folder called main.py. You can import your helpers just like you did with the math module!
File: main.py
import my_helpers
result = my_helpers.add(10, 5)
print(f"The total is {result}")
3. The __name__ Protection
Sometimes you want a file to be both a standalone script AND a module. But if you have print statements in your module, they will run as soon as you import it. To prevent this, we use this special Python idiom:
def useful_func():
print("Doing work...")
if __name__ == "__main__":
# This block ONLY runs if you run this file directly.
# It will NOT run if you import this file as a module.
useful_func()
4. Why Use Custom Modules?
- Organization: Keep your "logic" separate from your "user interface."
- Reusability: Use the same
database_tools.pyin 5 different projects. - Readability: No one wants to read a file with 10,000 lines of code. Split it into 10 files of 1,000 lines instead.
Practice Exercise: The Geometry Library
- Create a file named
geometry.py. - Inside, define functions for
area_of_circle(radius)andarea_of_square(side). - Create a second file named
app.pyin the same folder. - In
app.py, import your geometry module and ask the user for inputs to calculate areas. - Print the results.
Quick Knowledge Check
- How do you import a file named
utils.pylocated in the same folder? - If you have a function
test()inside your module, how do you call it from another file if you usedimport my_module? - What happens to code placed inside the
if __name__ == "__main__":block when the file is imported? - Where should you store your custom modules so they are easy to find?
Key Takeaways
- Any
.pyfile can be a module. - Modules allow you to share code between different files.
- Use the
if __name__ == "__main__":block to keep your modules clean. - Modular code is easier to maintain, test, and read.
What’s Next?
We know how to make our own modules, but Python already comes with a treasure chest of them. In Lesson 9, we’ll deep-dive into the Python Standard Library to see what "batteries are included"!