Introduction to Python ImportError
In Python, we use “import” when we want to import any specific module in the Python program. In Python, to make or to allow the module contents available to the program we use import statements. Therefore when importing any module and there is any trouble in importing modules to the program then there is a chance of ImportError occurrence. In this article, we will discuss such error which generally occurs if there is an invalid declaration of import statement for module importing and such problems also known as ModuleNotFoundError in the latest versions of Python such as 3.6 and newer versions.
Examples of ImporError in Python
In Python, when we want to include the module contents in the program then we have to import these specific modules in the program. So to do this we use “import” keyword such as import statement with the module name. When writing this statement and the specified module is not written properly or the imported module is not found in the Python library then the Python interpreter throws an error known as ImportError.
There are two conditions when the ImportError will be raised. They are
- If the module does not exist.
- If we are trying to import submodule from the module
Now let us demonstrate in the below program that throws an ImportError.
Example #1
Now suppose we are trying to import module “request” which is not present or saved in Python drive where we need to download it. Let us see a small example below:
import request
Output:
In the above sample code, we can see that we are importing a module named “request” which is not present in the downloaded Python library. Therefore it throws an ImportError which gives the message saying no module named “request”. As each module when downloaded or inbuilt it has its own private symbol table where all defined module is saved by creating separate namespace. Therefore if the module is present then there is no occurrence of such error.
In Python, there is another way of importing modules in the program and if this statement also fails then also ImportError occurs. Let us see the example below:
Example #2
from crypt import pwd
Output:
In the above program, we can see another way of importing modules. This also throws ImportError if the module is not present in the private Python library.
The above two methods of importing modules in the program throw an error if the module is not present. Therefore catch such errors in exception handling concept it provides an ImportError exception which has the Python exception hierarchy as BaseException, Exception, and then comes ImportError. In Python, even moduleNotFoundError is also the same as an ImportError exception. Now let us below how to handle such error in the Python program using try and except blocks of exception handling.
Example #3
print("Program to demonstrate to handle ImportError:")
print("\n")
try:
from crypt import pwd
except ImportError as ie:
print("It cannot import module and submodule", ie)
Output:
In the above program, we can see when we are trying to import the “crypt” module and we are handling this ImportError using try and except the block of exception handling. This is one way to avoid the error message to be printed.
To avoid such an ImportError exception we saw above the use of exception handling. But still, it will display the error message on the output screen. Such error occurs when there is no module present in the Python private table. To avoid this we can directly download this module from the Internet to the Python IDE. Let us see how we can avoid this ImportError we need to download the module and we will not get this error and this done as below:
pip install module_name
If pip is installed we can directly run this to install the module. Else first we need to install the pip and then install the other packages.
Another way to download the module is to download the package directly through the Internet by downloading the module and unzip the folder and save that folder where the Python software is saved.
Conclusion – Python ImportError
In this article, we conclude that the ImportError is an exception in Python hierarchy under BaseException, Exception, and then comes this Exception. In Python, ImportError occurs when the Python program tries to import module which does not exist in the private table. This exception can be avoided using exception handling using try and except blocks. We also saw examples of how the ImportError occurs and how it is handled.
Recommended Articles
This is a guide to Python ImportError. Here we also discuss the introduction and working of import error in python along with its different examples and its code implementation. You may also have a look at the following articles to learn more –