Python Day #9: How to Create and Import Python Modules (Step-by-Step Guide)
Learn how to organize Python code using custom modules. Master importing functions across files to write clean, reusable, and professional Python programs fast.
At the stage you have been writing your own blocks of reusable code over the last couple weeks, you’ve honed in your skills and understand the real reasons we define our own reusable blocks of code, a.k.a functions.
The next level to truly understand merging python files together, and taking the next step to reusability is learning how we create our own modules in our code. What is a module? How do you define your own?
Welcome back to the Python Roadmap to Success, where I am giving all my readers a fair chance at learning Python the right way, following a proven roadmap that I’ve structured and taught for over the last four years.
In our last article, we covered the eighth phase in the Roadmap to Success, it covered How Functions call other Functions. This lesson will help you reinforce everything you’ve been learning, but also build out your level of understanding so we can write modules.
In this lesson, you are going to learn how and why we create our own modules as well as how we import these into our main code to get everything running the right way. This is the next level of code reusability.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python. Join us today!
We know now that, functions help you keep your code clean, organized, and reusable. But now, we don’t want messy files, we don’t want all our functions in the same place. The goal is to separate them into their own files and import them to use them when we need to.
Over the last 8+ years I’ve been teaching full-time around the world which has given me the unique opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time. Breaking down complex topics, step by step.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
In this article, we’re going to build on what you’ve already learned with reusable code, while now expanding your skillset to work with modules. Last time, you worked on having functions call other functions. Now, it’s time to practice and build on that. The goal is to have you start writing modules that can interact together.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
👉 Only for two more weeks, I am offering my readers a 30% off on all annual subscriptions! Show your parents you care about your career, thanks mom and dad…
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
P.S - Drop any questions in the comments or feel free to DM me directly as I’m always happy to help you guys out.
What is a Module?
Last lesson, I set the stage for how you can define your own functions. You were introduced to the keyword def
, this just means define and we use this at the start when we are going to define/declare a new function in our code.
Writing your own functions the first level of code reusability, this is the starting point as you will be writing your own functions all the time to complete certain tasks in your programs.
Now we want to level up, because we don’t want to have 100% of our code in a single file. This is for a few reasons, one it gets messy so it’s hard to read and understand. Two, when it comes to debugging it makes it harder to find bugs if everything is in one file.
Instead of creating only one python .py
file, you can create as many as you want as long as they are in the same directory. This would mean that you have your standard main.py
file, then you may have another file called, calculations.py
.
As of now, these are two completely unrelated files. The main.py
file will be the main file, this is the only real file you will run. This file will then automatically call and utilize other code from other python files, so you don’t need to run every file.
Then our calculations.py
file could store all the functions we have written. If we image we have some functions for handling mathematical calculations, we could store them all here.
In order to use them we need to give the main.py
file access to the functions, and call those functions in our main.py
file. This keeps the main file clean and easy to read without also have to store the entire function definition.
This is the next level of code reusability. You should be writing well documented Python modules, not only can you structure out your code in a professional manner, but once you write you own module, you’d be able to use this in other projects as well.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
How Do We Write a Python Module?
Okay, now this might seem hard but it is rather straight forward. First you need to create another Python file apart from main.py
. For my example today, I will make a file called, calculations.py.
Inside this file I will keep it nice and easy. I will define four functions to handle basic math. Yes, there are plenty of other ways to do this but I want you to easily grasp this topic, then you can think back to your code. Think back on how you could revise your current Python script to store your own functions in modules.
Now in my above code, you can see in the Python file there are only functions. I defined all the functions I want in this separate file. At the bottom of the file I am simply running the functions to make sure they work how I think they do.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
The next part would be bringing this file into our main.py
file so we can use these functions in other parts of our code. To do this we head into our main file and at the top of the code we import the file just like I have below.
Have you read the code above? I aimed to use tactics we have already looked at in the Python Roadmap to Success. Firstly, you can see I imported my python file calculations with the line, import calculations
. There are other ways to do this we will look at in a minute.
Now when I run this file which is my main.py
file, this gives me access to all the python functions I wrote in calculations.py
. The program begins with asking me for some math symbol, then I enter two numbers.
I pass these as arguments to the function I defined which I called do_calculations
. The do_calculations
function runs and calls one of my other defined functions based on the operator I entered during input.
This will return to us a number that I then print off in the final few lines of code. The above is a great starter example for all of you guys starting out on how we can separate our code into modules to better optimize and store our scripts.
This would work the same as if I had it all in one file, but remember as a developer the goal is to write clean, readable and reusable code. Therefore this is the next leg up after we learn how to define functions, we then begin to create our own modules.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Ready to Take Your First Steps with Python?
Most people get stuck before they even start. But you don’t have to!
I’ve put together a Free Course: The Pure Basics of Python that covers the very basics of Python, made just for beginners like you.
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years!
Here’s what you’ll learn:
✅ Step-by-step help to set up VS Code and install Python.
✅ Learn how to use variables, built-in functions, and even nesting functions.
✅ A quick test to see how much you’ve picked up.
✅ My handcrafted Python Guide to keep you on track.
This free course is a perfect first step — you’ll get a feel for my teaching style and build confidence to keep going.
👉 Ready to get started?
P.S. — If you like the free course (and I think you will!), I’ll show you how to dive deeper with my full Python Masterclass.
🎁 - Get a free one-on-one coaching session with any Masterclass!
Go through the entire Masterclass, complete all material, and attend the Q&A's, if you still feel like your struggling I'll personally work with you one-on-one until you're confident!
The Three Ways to Import Code
Finally, let me cover the different ways to import your code and the benefits to each one. There are three ways to do this and use will inevitably see them in your programming careers, so it’s important to know how they work and why choose one over the other.
1. Import the entire module
This first way is the same way I did in the previous code example. We just use the line import module_name
. This is great when you are just beginning your coding career as well as when you are working on larger projects.
import calculations
Anytime you want to use a function from the imported module you must first state the module_name
followed by the function you want to use. For example: calculations.addition(10,20)
. This means a little more typing, but this will prevent confusion between functions and modules as your projects grow.
2. Import a Single Function
Sometimes you do not want access to the entire module. You only need one function from the module, so there is no real reason to import the entire module. We are limiting the possibility of errors while also speeding up our code.
To import a single function all we need to do is:
from calculations import addition
Now this is great, and you will see this a lot as you start working with other frameworks in Python. To use the addition
function now I can just call the function by itself without needing to link the calculations module before the function as in the first scenario.
3. Import Everything
This final way may seem the easiest, but while it is the easiest it should also be avoided when possible. Just as the first import technique we can import the entire module by saying import module_name
.
But then you will need to link the module_name
before the function you want to use. This is fine, as it will help you keep track of where everything is coming from. The final technique is to import every function from the get-go.
from calculations import *
This literally translates to: “from the calculations module, import every function”. You can then use any function from the module without needing to link the name of the module. So if we wanted to do subtraction, I could just call: subtraction()
.
Try to avoid this though as this will cause errors and confusion especially as your programs grow in size. My recommendation is to stick with the first or second import technique as these are best practices in Python.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass platform designed from 8 years teaching experience and that has helped over 1,500+ students succeed with Python. (All my readers unlock a limited time, 35% discount, use code: pythonsummer35)
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
The Nerd Nook: This is where you are right now! I write here multiple times per week with the sole goal of breaking down this larger than life topic to help you excel and learn Python faster than you thought was possible.
This article will be a piece to a larger roadmap but will go behind a paywall after 6 weeks! To help support my work in teaching others consider joining as a premium reader to unlock all these articles, monthly Python projects and in-depth dives.
Wrapping Up Lesson Nine
By creating your own modules, you can break your code into smaller, cleaner pieces—each stored in its own file. This helps make your programs easier to read, easier to manage, and a whole lot easier to reuse as your projects get bigger and more complex.
Think of it like this: instead of stuffing everything into one big messy script, you're giving each part of your code its own space to live. That way, your main file stays focused on the big picture, while your other files hold all the details—like the functions you've already written.
It’s a simple change, but it has a huge impact on how clean and professional your code feels.
Don’t stress about memorizing it all right away. The more you write and the more you practice, the more natural this will become. These are the habits that will stick with you as you grow into a confident Python developer.
This is a big milestone in your learning journey. Understanding how to structure your code properly is what separates beginners from those who are ready to build real projects.
Thanks for being here and learning with me. You’re doing great—now take what you’ve learned, and keep building. This is how real progress happens.
If you're enjoying this step-by-step roadmap, be sure to check out the premium version. You’ll get access to full-length tutorials, real-world Python projects, downloadable code, and deeper lessons that help you truly master the material.
Let’s keep moving forward with Day #10 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.