Learn Python in 30 Days — Day 20: Creating Your Own Module
Day 20: Creating Your Own Module
Welcome to Day 20 of the Learn Python in 30 Days series!
Yesterday, you learned how to use Python’s built-in modules like math, random, and datetime.
Today, you’ll level up again by creating your own module a huge milestone on your way from beginner to “I can build actual programs”.
This is where your Python projects become organised, reusable, and far easier to maintain.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
By the end of this lesson, you’ll know how to:
-
Split code into multiple files
-
Import your own functions
-
Build a tiny reusable module
-
Understand how Python finds modules
-
Use
if __name__ == "__main__"properly
Step 1 — What Is a Module?
A module is simply a .py file you can import.
That’s it.
Built-in modules like math and random are written in the same way, you just don’t see the source.
Your own code can be imported the same way:
If you can write functions, you can write a module.
Step 2 — Create Your First Module File
Let’s build a simple utility module for a game something you might use in your Tiny Dungeon (Day 14) or future Python projects.
Create a file called tools.py
Inside it:
You’ve just made a module!
Import Your Module in Another File
Now create a second file called main.py:
Run it:
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub main.py and tools.py and run it yourself.
This is real, professional program structure, split code in files, import what you need.
How Python Finds Your Module
Python looks for modules in this order:
-
The current folder (same directory as your script)
-
Installed site-packages (pip installs)
-
Standard library modules
As long as tools.py is in the same folder as main.py, the import will work.
Step 3 — Importing Specific Functions
Instead of importing the whole module, you can pick and choose:
Or rename them:
Clean, readable, flexible.
Step 4 — Using __name__ == "__main__"
This special block lets your module act as:
-
A library when imported
-
A standalone program when run directly
Add this to the bottom of tools.py:
Run it:
→ Runs the test code.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Import it:
→ Does not run the test code.
This is a core Python pattern you’ll use for the rest of your programming life.
Mini-Project — Build a Text Helper Module
Create a module called textutils.py with:
-
A
banner(text)function -
A
box(text)function -
A
warning(text)function
Example:
Then use it inside a separate file:
This small module can be reused in all your CLI tools going forward.
Wrap-Up — Why This Matters
Today you learned how to:
-
Make your own Python module
-
Import it into another file
-
Organise multi-file programs
-
Use
__name__ == "__main__" -
Build reusable functions that stay clean and separate
You’ve just stepped into the world of real software structure — the kind that powers actual applications, not just practice scripts.
Next Up — Day 21 – Week 3 Challenge
Tomorrow is Day 21 — Week 3 Challenge, where you’ll put modules + functions + files + error handling together into a Daily Journal CLI App.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty



Comments
Post a Comment