Skip to content

How to create a module in Python | Example code

  • by

The python module is a set of Python statements, functions, and definitions that you want to include in your application. It could be any py file in the project. To create a Python module just save the code in a file with the file extension “.py“.

Example of how to create a module in Python

Simple example code.

Save this code in a file named

var = "Hello main file"
num = 9876543210

def greeting(name):
    print("Hello, " + name)

How to import and use the modules in Python

There are multiple ways to do it, see below one of using the import file. Import the module named main, and call the greeting function:

More ways to import variables or modules:- Python import

import main



Hello, John

Complete code structure

How to create a module in Python

Do comment if you have any doubts or suggestions on this Python module tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *