Javatpoint Logo
Javatpoint Logo

Writing a Python Module

It seems to be a bit difficult to create your own Python module, but what if we say that creating or writing a Python program is a very simple task.

In this tutorial, we are going to write a Python module, and after writing it, we will also use it inside a Python script. But before all of this, let's first understand that what a Python module is.

Python Module

A Python file that contains various statements and definitions is called a Python module, and it can have variables, classes and various functions in it. Like a simple Python script, a runnable Python code is also present inside the Python module.

Python module becomes more useful when we group the relatable code and use them as a module inside a Python program, and with the help of a Python module, we can even make our code logically organized. We can use a Python module inside a Python program by simply using the 'import' statement.


Suppose, in a Python module, i.e., module1, three functions present in it, and we want to use only one of them in our code then we can also import only a single function by using from a statement with import like as we used below:


As we have learned about the Python module and have a basic idea that how a Python module works and is imported into a program, now we will move forward to the writing part of it.

Writing a Python Module

In this section, we are going to write a Python module and understand that how we can use the module, we have written inside a Python program.

We can create or write a Python module just like we create a Python script, and better to say; we can even use a Python script as a Python module inside another Python script. But this will only be valid if the Python script which we will be using as a Python module is present in the same location where the Python is installed in our system.

So, let's create a Python script first and save it with the '.py' extension and save it at the same location where Python is present. Follow the following given steps to write and create a simple Python module:

Step 1: Writing a Runnable Program:

First, we have to write a runnable program inside the Python shell by opening a new file in it. For example, we will write the following addition function program inside the Python file:

Step 2: Saving the File:

Now, after writing the addition program as given in the first step, we will save the file with the '' name in the default directory assigned by Python.

Writing a Python Module

Now, we have created an 'adding' module, and this can be imported into any Python program to use its addition function for adding two variables. Let's use the 'adding' script as a Python module to perform addition operation in the following example program:


Enter first integer variable: 26
Enter second integer variable: 24
The sum of two integer variables given by you is: 50


In the above program, we have performed the addition operation on the two input variables we have taken from the user. We have used the addition function of adding module by importing this module inside the program. In last, we printed the addition result of the two variables in the output and ended the program.


We can create or write a Python module by simply writing a Python program with functions and classes in it, and then we can use the module we have created inside another program by simply importing it inside the program. We can even use a single function from the program by using an additional 'from' keyword with the import statement. With the help of a Python module, we can make our code logically organized and group similar code together in a single module.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA