How to make your Python module

Build your own Python module w/ an example: Learn how to create and use your own custom Python module for your project.

Creating a Python Module

Creating a Python module is a relatively easy process. In this tutorial, we will show you how to create a module for Python. To create a module, you need to have a .py file inside a folder. This folder and all its contents will be treated as a module by Python.

Let's create a folder called mymodule, and inside this folder we will create a file called mymodule.py. This file will contain all the code for our module.

# mymodule.py

def hello():
    print("Hello World!")

def bye():
    print("Goodbye!")

This is a very simple module, with two functions hello() and bye(). Now, to use this module, we need to import it in our Python program.

# main.py

import mymodule

mymodule.hello()
mymodule.bye()

Now, when you run main.py, it will print out Hello World! and Goodbye!. Congratulations, you have successfully created your first Python module!

Answers (0)