Modules are used to store code, libraries, function in different and separate files. In order to use a module, it should be imported to the current code page properly. Not imported modules can not be used. So Python provides the import mechanism in order to import and use modules. These modules can be built-in modules provided by Python or 3rd party modules provided by other vendors or developers. In this tutorial, we will learn how to import a module in different ways in the Python programming language.
Import Module
Importing a module in Python is very easy where we will use the import
statement and provide the module we want to import. This module can be a built-in Python module provided by default or a 3rd party module installed later by using the package manager or pip command. In the following example, we will import the modules named, math, random.
import math
import random
import wisetut

We can see that the existing modules like math and random are imported properly without a problem or error. When we try to import module wisetut we got an error named ModuleNotFoundError . Because this module couldn’t found in the Module path or directory.
Import Module with By Aliasing (Different Name)
Modules can be named according to the creator of the module. But we have the ability to change the name of the imported module into different ones for different reasons. This is called aliasing where module can be called with aliases even the module name stays the same. In the following examples, we will import the math module as m and random module as ran.
import math as m
import random as ran
m.ceil(1.5)
m.floor(1.5)
ran.randrange(10)
ran.randint(0,10)
Import All Objects (Functions, Variables) From Module
By default, the import will import the module. In order to use module objects like functions, variables, etc. the module name should be used. But if we want to use module object, function, and variables without using the module name we should import all of these with the glob *. Below we import all module objects of the math module. Keep in mind that this can create object name collusion if there is already the same-named object it will be overwritten and can not be used directly.
from math import *
from random import *
ceil(1.5)
floor(1.5)
randrange(10)
randint(0,10)
Import Subpackage or Submdule
Modules under anotother module is called Subpackage or Submodule. They have parent child relationship and while important the parent module should be specified by delimiting point. Below we will import some submodules or subpackages.
import wisetut.page
import wisetut.page.test
Import Relatively
Relative path is used to load modules from different directories which is generally upper directories. Leading dots are used to specify upper directory in a module. As modules can provide submodule or subpackages they can be imported with relative import.
from ..module1 import ModuleY
from ..module1.module2 import ModuleZ
List Module Objects (Functions, Variables) with dir() Method
Every module contains multiple objects which are generally classes, functions and variables. These objects and provided via modules and if we want to get information without importing them dir() method can be used. dir() method is designed to provide information about the given module. dir() method can be also used from Python interactive shell practically.
dir(random)
dir(math)
