Importing Modules

A module simply refers to a file with python code. Furthermore, the python code can be available in the form of functions, variables or class defined. Moreover, importing modules in python can be said to be similar to #include header_file in C/C++.

importing modules

                                                                                                                       Importing Modules

How to Create Module in Python

Below is the folder structure that is used to test the code:



Before understanding how to import modules in python, one must first learn creating them. With regards to importing modules, following are the steps for creating a module in python.


Step 1) Create a file and name this file as

Step2) Inside, create a function- display_message()

Def display_message():

return "Welcome to Tutorials!"

Step 3) Now make another file

Step 4) Inside, import, the file:

import test

While importing, just mention the file’s name and there is no need to mention the

Step 5)

Then one may call the function display_message() that is from inside One would need to make use of module_name.function_name.

For example test.display_message().

Import test


Step 6)


On executing, one would attain the following output:

Browse more Topics Under Functions

Importing Entire Module

import module_name
When use of import takes place, it searches initially for the module in the local scope by calling __import__() function. The reflection of the value returned by the function takes place in the output of the initial code.


import math




Importing Selected Object

import module_name.member_name

In the above code, importation of the module math takes place. Furthermore, accessing its variables is possible by considering it to be a class and pi as its object.
Also, the return of the value of pi is by __import__().
Moreover, importing the pi as a whole can take place in the initial code, rather than importing the whole module.


from math import pi

# Note that in the above example,

# we used math.pi. Here we have used

# pi directly.



Using Absolute Imports

After understanding how to import a file as a module inside another file, one must learn the management of the files in folders. Furthermore, the importation of the files in the folders can take place either by using absolute or relative imports. These two types are important when it comes to importing modules.

Consider a project folder structure for the purpose of importing modules, so

The root folder is the project/ that involves two subfolders package1 and package2.

There are two modules, and, in folder package1.

The folder package2 is characterized by one class, a sub-package subpkg with Moreover, last is

  • In, there is a functioncalledmyfunc1.
  • Also, in, there is a functioncalledmyfunc2.
  • Moreover, in, there is a functioncalledmyfunc3.
  • Furthermore, in, there is a functioncalledmyfunc4.

For Absolute imports, the requirement is to add the entire path of the module right from the project root folder.

Furthermore, now is the time to make use of absolute imports to refer to the functions present in each of the module.

Moreover, to work with the functionmyfunc1, the requirement is to import as follows:

from package1.module1  import  myfunc1


from package1 import module1


Also, to work with the function myfunc3 you will need to import as follows:

from package1.subpkg.module3  import  myfunc3


from package1.subpkg import module3


Using Relative Imports

Considering the same folder structure as above, let’s see how to import the same using relative imports.

In relative import, the module whose importation has to take place is relative to the current location. Also, the current location is where there is the presence of the import statement.

In relative imports, there is a requirement to add a period (.) before the module name when importing takes place using from.

It will be 2 periods (..) before the module name in case the module is in the one level up from the current location.

Referring to the folder structure figure, one would have the following modules along with their function, which we need to refer to.

  • In, there is a functioncalledmyfunc1.
  • Also, in, there is a functioncalledmyfunc2.
  • Moreover, in, there is a functioncalledmyfunc3.
  • Furthermore, in, there is a functioncalledmyfunc4.

To work with the functionmyfunc1, there is a need to import as follows:

from  .module1  import  myfunc1

To work with the function myfunc3, there is a need to import as follows:

from  .subpkg.module3  import  myfunc3

FAQs For Importing Modules

Question 1: What is meant by a module in python?

Answer 1: In python, one of the programming languages, a file is considered a module. Furthermore, in order to use the module, one would have to import it using the import keyword. Also, one can use the variables or function present inside the file in another file by importing modules.

Question 2: Explain the significance of modules?

Answer 2:  A module allows one to logically organize the python code in a logical manner. Furthermore, grouping related code into a module makes the code convenient to use as well as easier to understand.

Moreover, importing modules is certainly possible. Also, there exist some built-in modules in python.

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.