Skip to content
beginner

Writing Your First Python Module

Are you ready to take your Python skills to the next level? As your projects grow, organizing your code becomes more important—and that’s where modules…

Published 2026-05-11Updated 2026-05-128 min read

Are you ready to take your Python skills to the next level? As your projects grow, organizing your code becomes more important—and that’s where modules come in. Learning how to create a Python module is a key step toward writing professional, job-ready code. In this beginner-friendly python module tutorial, you’ll discover how to organize your code into separate files, making your programs easier to manage, reuse, and share.

Let’s dive in and see how you can start building your own Python modules today!


What Is a Python Module?

A Python module is simply a file containing Python code. Think of it as a toolbox: you put useful functions, variables, or classes inside, and then you can use them in other programs whenever you want.

Modules help you organize your code into logical, manageable pieces. Instead of having one giant file, you can split your code into smaller files, each focused on a specific task.

You’ve already used modules, even if you didn’t realize it! For example:

import math
print(math.sqrt(16))  # Uses the sqrt() function from the math module

Here, math is a built-in Python module. In this python module tutorial, you’ll learn to create your own.


Quiz Question 1

Question: True or False: A Python module is just a .py file containing code.

  • A) True
  • B) False

Why Organize Code Into Modules?

As your programs get bigger, good python code organization becomes essential. Here’s why modules are so helpful:

  • Keep Code Tidy: Splitting your code into modules makes it easier to read and understand.
  • Reuse Code: Write a function once, put it in a module, and use it in many projects.
  • Easier Debugging: If something goes wrong, you only need to check the relevant module instead of searching through a huge file.
  • Teamwork: Modules make it easier for teams to work together, with each person focusing on different parts of a project.

Learning to create and use modules is a skill every Python programmer needs!


Quiz Question 2

Question: Why should you use modules in your projects?

  • A) To keep code organized
  • B) To reuse code
  • C) To make debugging easier
  • D) All of the above

How to Create a Python Module

Let’s walk through the steps to python create module and use it in your projects.

1. Create a New .py File

A module is just a Python file. Open your code editor and create a new file named greetings.py.

2. Write Some Functions

Add a couple of simple functions to your new file:

# greetings.py

def say_hello(name):
    """Greet someone by name."""
    print(f"Hello, {name}!")

def say_goodbye(name):
    """Say goodbye to someone by name."""
    print(f"Goodbye, {name}!")

3. Save and Name Your Module

Save the file as greetings.py in the same folder as your main Python script. The file name (without .py) will be the name of your module.

That’s it! You’ve just created your first Python module.


Importing Your Module Into Another File

Now, let’s use your custom module in another Python file. This is called a python file import.

1. Create a New Python File

Make a new file in the same folder, called main.py.

2. Import Your Module

At the top of main.py, import your module using the import statement:

import greetings

3. Call Functions From Your Module

Now you can use the functions you wrote:

greetings.say_hello("Alice")
greetings.say_goodbye("Bob")

When you run main.py, you’ll see:

Hello, Alice!
Goodbye, Bob!

Quiz Question 3

Question: What is the correct way to use a function from your custom module named greetings in another Python file?

  • A) import greetings; greetings.say_hello('Alice')
  • B) include greetings; say_hello('Alice')
  • C) require greetings; greetings.say_hello('Alice')

4. Common Import Mistakes

  • File Not Found: Make sure both files are in the same folder, or set the correct Python path.
  • Misspelled Names: The module name must match the file name (without .py).
  • Circular Imports: Don’t try to import modules that import each other—it can cause errors.

If you see an error like ModuleNotFoundError, double-check your file names and locations.


Quiz Question 4

Question: What will happen if you try to import a module that doesn’t exist?

  • A) Python will create it for you
  • B) You’ll get a ModuleNotFoundError
  • C) It will be ignored

Best Practices for Modules

Good python code organization makes your code easier to use and share. Here are some tips:

  • Choose Clear Names: Name your modules and functions so it’s obvious what they do. For example, greetings.py for greeting functions.

  • Use Comments and Docstrings: Add short descriptions to your functions and modules. This helps you (and others) remember what everything does.

    def say_hello(name):
        """Greet someone by name."""
        print(f"Hello, {name}!")
    
  • Keep Modules Focused: Each module should have a single purpose. If you find yourself adding unrelated code, consider creating another module.

  • Don’t Repeat Yourself: If you use the same code in several places, put it in a module!

  • Document Your Module: At the top of your module file, you can add a comment or a docstring describing what the module does.

    """
    greetings.py - Functions for greeting and saying goodbye to users.
    """
    

Frequently Asked Questions

Do I need to name my module file the same as the functions inside it?
No, the file name (without .py) is the module name. Your functions inside can have any names you like.

What happens if my module and my main script are in different folders?
Python looks for modules in the same folder as your script and in some special system folders. If your module is elsewhere, you may need to adjust the Python path or use a package structure.

Can I import more than one custom module in the same script?
Yes! You can import as many modules as you need, both built-in and your own.

If I change the code in my module, do I need to restart my program to see the changes?
Yes, if you’re running your script normally. In some development environments, you may need to reload the module or restart the interpreter.

What is the difference between a module and a package?
A module is a single .py file. A package is a folder containing one or more modules (and a special __init__.py file).

Can a module contain variables and classes, or only functions?
Modules can contain functions, variables, classes, and even other imports.

Why do I get a 'ModuleNotFoundError' even though my file exists?
Check that your file is in the correct folder, the name matches, and there are no typos.

Is there a limit to how many functions I can put in a module?
No strict limit, but it’s best to keep modules focused and not too large.

How do I add comments or documentation to my module?
Use comments (#) and docstrings (""" ... """) to describe your module and its contents.

Can I use built-in modules and my own modules together in the same script?
Absolutely! You can import both in the same file.


Next Steps: Using Modules in Real Projects

Now that you know how to python create module, you can start using them in your own projects. Here are some ideas:

  • Calculator Module: Put math functions (add, subtract, multiply, divide) in a module and use them in a calculator app.
  • Text Utilities: Create a module with functions to format or analyze text.
  • Game Helpers: Write a module with functions for your favorite games.

As you grow, you’ll hear about packages (collections of modules in folders) and the Python Standard Library (a huge collection of built-in modules). These are exciting topics for the future!

Want to learn more? Check out the official Python documentation on modules or keep exploring tutorials here on learnpyfast.com.


Quiz Answer Key

Question 1

Correct answer: A) True

Explanation: A Python module is simply a .py file containing code.


Question 2

Correct answer: D) All of the above

Explanation: Modules help keep code organized, make code reusable, and make debugging easier.


Question 3

Correct answer: A) import greetings; greetings.say_hello('Alice')

Explanation: You import your module with import greetings and then call its functions using the module name, like greetings.say_hello().


Question 4

Correct answer: B) You’ll get a ModuleNotFoundError

Explanation: If you try to import a module that doesn’t exist, Python will raise a ModuleNotFoundError.


Keep Going!

Congratulations! You’ve learned how to create and use your own Python modules—a big step toward writing professional, organized code. Try making your own modules and using them in small projects. As you practice, you’ll see how modules make your Python journey smoother and more fun.

Keep exploring, keep coding, and remember: every great Python project starts with well-organized code!

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

beginner7 min read

Defining Functions in Python

Are you tired of writing the same lines of code over and over? Do you wish there was an easier way to organize your programs and make them easier to read?…

Read tutorial
beginner8 min read

Importing Modules in Python

Learning Python is like opening a toolbox full of handy gadgets. Many of these gadgets are called modules. If you want to write real programs,…

Read tutorial