How to make a module in Python

Learn how to create a Python module with an example & step-by-step instructions.

Creating a Simple Module in Python

Creating a module in Python is very simple. A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. A module can define functions, classes and variables. A module can also include runnable code. Here's an example of a simple module that contains a function called helloWorld():


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

To make use of this module, we must first import it. We do this with the import statement. Here's an example of importing the helloWorld() function:


import helloWorld

helloWorld.helloWorld()

The output of this code is:


Hello World!

This example shows how easy it is to create and use a module in Python. Once you understand the basics, you can use modules to organize your code and keep your projects organized.

Answers (0)