Writing and Using Custom Modules

Introduction

Up to now you have probably written each program as a single Python file. As your engineering scripts grow, you will find yourself copying the same unit-conversion function, material constant, or plotting helper from one file to the next. A custom module solves this: any .py file you write can be imported into another script, so the functions and constants you define once become reusable tools across every project you build.

This topic walks through the full workflow. You create a module file with a sensible name, group related functions and constants inside it, add docstrings so others (including future you) know how to use it, and then import it from another script using import or from ... import. A key piece is the if __name__ == '__main__' guard, which lets a file behave as both a runnable program and an importable library: code under the guard runs only when you execute the file directly, not when something imports it. Without the guard, your module's test or demo code fires every time it is imported, which is a common and confusing bug.

The videos below focus on the two parts that trip students up most: understanding what __name__ actually is and why the guard matters, and organizing code sensibly across files. While watching, pay attention to what gets printed when a file is run directly versus imported, how module-level code differs from code inside functions, and how splitting code into separate files changes the way you call it.

Video 1: if __name__ == "__main__" in Python | Python Tutorial - Day #45

CodeWithHarry · 9:24 · 373,338 views · 3y ago · captions — Watch on YouTube

About this video

A tutorial-style walkthrough of the if __name__ == '__main__' statement in Python, part of a longer beginner Python series. It should explain what the built-in __name__ variable holds, how its value changes between running a file directly and importing it, and how the guard is written in practice. Expect live code demonstrations with two files showing the difference in behavior.

Why this video

At just over nine minutes it gives the guard enough time for a full worked example, and CodeWithHarry's step-by-step beginner teaching style suits students meeting modules for the first time.

Video 2: What does if __name__ == '__main__' do in Python?

Tech With Tim · 5:08 · 87,974 views · 1y ago · captions — Watch on YouTube

About this video

A concise explanation of the same construct, aimed at clearing up why the line appears in so much Python code. It likely demonstrates importing a file and observing which code executes, then shows how the guard restricts the demo or test code to direct execution only. The short format makes it a quick reference or review.

Why this video

Tech With Tim is a well-regarded Python channel, and this five-minute version is a good second pass or refresher if you want the concept stated compactly after the longer tutorial.

Video 3: 5 Tips To Organize Python Code

Tech With Tim · 12:16 · 342,490 views · 4y ago · captions — Watch on YouTube

About this video

Five practical tips for organizing Python code, which should touch on splitting work into functions, separating code across files and modules, using constants rather than magic numbers, and keeping related logic grouped together. The advice is general-purpose rather than tied to one construct, so treat it as guidance you apply when deciding what belongs in your module.

Why this video

It directly supports the organizing functions and constants subtopic and helps you make good design decisions about what to pull out of a script and into a reusable module.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.