How to Write Module in Python 3.8

 A built-in module in python is containing the number of functions. Actually a module is a python file. Python put the built-in functions in the module files user can use these module when he want.  In python you can create your own module according to your application requirements.

In the previous tutorial we have discuss about how to create method in python. today we will discuss in this tutorial how to create module in python.

We will cover followings two types of module:-

  • How to use built-in module in python
  • How to write module in python
  1. Use built-in module in python:

To use any built-in and own created modules in python file we must use the import statement to access the specific module class. Without executing the import statement we can’t access any source file in another file.

When the interpreter encounters the import statement, it imports the module if the import module is present in directory. When you import the desired module its automatically list on search path.

For example if we want to import the math module in our python file , we need to put the following statement on the python namespace.

# How to use built-in module in python.
# importing the module
import math

The from import statement

The * simple use with the from keyword to import the statement its mean you access  all the functions with created in the module.


from random import *

If you know the exactly what you will need to import method or function from the module. No need to declare the * after import. It will return you all the function which include in module.



OutPut is:

[‘__call__’, ‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__name__’, ‘__ne__’, ‘__new__’, ‘__qualname__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__self__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__text_signature__’]

The dir() function:

The dir() use to access and sort all the list which include in the module by name. the list contain the name of functions, method, variables, strings which are define in the module.


import math


[‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘comb’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘dist’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘isqrt’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘perm’, ‘pi’, ‘pow’, ‘prod’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’]


Import date time module in python

import datetime
from datetime import date



create module in python

2. How to Make You Own Module in Python?

To write module in python or make your own library for your project which contain classes, methods, functions and variables to organize your applications. Once you have make module library it’s easy to import and access in any file anywhere you want.

We will discuss custom module briefly with simple examples. So first you create a fresh project in your Pycharm IDE and you project have the following directory and files.

write module in python

we write module in python with very basic example. Python module library is useful to manage the project. In this article we will see how to import custom module in your project.

Here I will  write the basic three function in file.  Add, Subtract and multiply. 

Step 01: Open your file and write the followings three function.

#python 3.8
#create module in python
# add function
def add(a, b):
return a + b

# subtract function
def subtract(a, b):
return a - b

# multiply function
def multiply(a, b):
return a * b

Step 02 : Open your file and import the myfunction module in namespace and  write print function to access the module functions like add, subtract and multiply.

#python 3.8
# How to use built-in module in python.
# importing the module
import myfunction

print(‘Add function :’, myfunction.add(4, 8))
print(‘Multiply function :’, myfunction.multiply(4, 8))
print(‘Subtract function :’, myfunction.subtract(4, 1))

The Output Will be:

Add function : 12
Multiply function : 32
Subtract function : 3

This Post Has One Comment

Comments are closed.