Functions

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:

                    

modtest/

test.py

display.py

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 test.py

Step2) Inside test.py, create a function- display_message()

Def display_message():

return "Welcome to Tutorials!"

Step 3) Now make another file display.py.

Step 4) Inside display.py, import, the moduletest.py file:

import test

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

Step 5)

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

For example test.display_message().

Import test

print(test.display_message())

Step 6)

 

On executing display.py, 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

print(math.pi)

Output:

3.141592653589793

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.

print(pi)

Output:

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, module1.py and module2.py, in folder package1.

The folder package2 is characterized by one class myclass.py, a sub-package subpkg with module3.py. Moreover, last is module4.py.

  • In module1.py, there is a functioncalledmyfunc1.
  • Also, in module2.py, there is a functioncalledmyfunc2.
  • Moreover, in module3.py, there is a functioncalledmyfunc3.
  • Furthermore, in module4.py, 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

or

from package1 import module1

module1.myfunc1()

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

from package1.subpkg.module3  import  myfunc3

or

from package1.subpkg import module3

module3.myfunc3()

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 module1.py, there is a functioncalledmyfunc1.
  • Also, in module2.py, there is a functioncalledmyfunc2.
  • Moreover, in module3.py, there is a functioncalledmyfunc3.
  • Furthermore, in module4.py, 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?
5th
6th
7th
8th
9th
10th
11th
12th
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.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.