In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure the programming code very efficiently. Import function allows us to reusable of code and making the past done work and projects maintainable.
Basically, Python basic import is well categorised into modules and packages and the module is defined as they are viewed into Python with the help of importing process and it is also defined as an object that comes up with an organizational unit of Python programming code.
Python only responds to the file containing the extension .py.
The power of modules is that they can be imported anytime and can be reused in other code as well.
>>>import math >>>math.pi
In this piece of code, we observed that we import the math module and use it and the second line we can access the pi variable which is within the math module. math is present and is part of the Python’s standard library which means it is universal in the Python and can be accessed and made available while working with the Python.
While importing we write math.pi rather than just pi because it is not only a module but also a namespace that keeps all the attributes of module together. It makes the code readable and organized in well manner.
We can import any module into the current program to use the functionality. If we want to import the module, we need to use the import keyword with the respective module name associated with. While interpreting the code, if import statement appears then it imports directly into our program. We can also use the functions inside a module by using the .dot () operator along with the associated module name.
For working of the code more efficiently, it is better to import one module only once per the interpreter window. If there is a chance in changing the modules we must again start the interpreter. And if we want test only one module iteratively just uses reload () with the module name with it.
There are 3 types of ways importing modules:
- From .. import statement
- From ..import * statement
- Renaming the imported module
From .. import statement:
This type is used to import only specific categorized functions and variables from a whole module instead of importing all the functions which are not necessary when choosing a particular function. It is used to import a specific kind of function from the main Python module.
>>>form math import add >>>Print (add(1, 2))
We can make the use of add () directly without implementing the module name. when it comes to the point of implementation of multiple attributes at a once, we need to separate the multiple attributes with just a comma and the syntax is followed as:
From calculation import add,sub
Here, add and sub are two multiple attributes separated by comma in between.
From ..import * statement:
This type of statement is a drawback in the Python language because if we implement this statement we disturb the concept of namespace and the function import* is when used, imports all the related functions and classes into the program. These may create a conflict among the other defined functions and other libraries that we imported.
The main disadvantage of this format is that code readability and hiding of the bugs within the program.
from calculation import * print(add(2, 3)) print(sub(3, 2))
Renaming the Imported Module:
We can rename the module that we are importing, and it comes to the use when the module name is large or wants to a give a short name to understand it easily. We need to use the keyword as and rename it.
import calculation as cal print (cal.add(1, 2))
Once the module is renamed, we can’t access the first named module because it’s not going to be recognised anywhere in the program.
Here, the calculation is renamed as cal and calculation is not accessible anywhere in the code.
After all importing the modules, if we want to list out all the names used in the particular module we need to use the dir() function which gives the list.