Python conditional import comes into use while importing modules. In python, modules are pieces of code containing a set of functions that can be imported into other code. Modules help in managing code by supporting readability. We don’t have to write the same piece of code every time. Instead, we can import the modules and avoid developing redundant codes. The syntax for importing a module is import module_name
.
But sometimes, when the module does not exist, or the importing was not successful, then python will throw ImportError. That is when conditional import comes into play. So using python conditional import, if a given import statement is not executing successfully, we can provide an alternate solution.
What is ImportError?
We use the keyword ‘import’ while importing a module. But sometimes, python will throw an error while importing a module. The error would be ImportError. As the name suggests, ImportError arises when python fails to execute the import statement. Usually, the import error arises in any of the two cases:
- If the module does not longer exist
- If you are trying to import a submodule from a module and the submodule does not exist in that module.
The syntax for importing a submodule from a module is:
from modulename import submodulename.
Need of conditional imports
While importing modules, it fails because when version changes occur in python, some functions are shifted from one module to some other module. Due to different versions, the module name of the module in which the function was present, also changes.
Also, if a particular dependency is missing, we can refrain from importing that module and prevent the failure of the code. If a module is to be used only if a particular condition is met, we would want to avoid importing that module if the condition is not met.
Some modules take a lot of import time. But, on the other hand, it will be a waste of computation time if the module is not going to be used at all. So, conditional imports come in handy when we want to avoid those scenarios.
Conditional import for version checking
When version changes occur in python, python may fail to import some modules. Because changes in version often mean that the location of a function in a module may have been shifted to another module.
Therefore, it is important to perform python conditional import by checking the version before importing a module.
For checking the version, we will have to import the sys module. Sys.version_info will check the python script version.
import sys
if sys.version_info > (version_number,):
import module1
else:
import module2
Here, it will first check the version. If the version is greater than the specified version, then python will import module1. If the version is less than the given version, then it will import module2.
In this way, we would import the correct module, which will be compatible with the python version.
Contional import for other conditional statements
We can also use python conditional import for handling other conditional statements. Like we checked the condition for python’s version, we can also do that for other conditions.
For example, we can choose to import a certain module only if a given variable has a certain value. And if the variable’s value is different, we would want to import some other module.
if var == value1:
import module1
elif var == value2:
import module2
In the above example, the value of the variable var is the conditional criteria. If the value of var is equal to value1, python will import module1. But, if the value of variable var is equal to value2, then python would import module2.
This way, we can avoid importing the wrong module and instead import the module which suits our program’s requirements.
Conditional import by exception handling
Another way of including python conditional import is by exceptional handling. The code will contain a try expression. If the execution of the code inside the try block fails, then python would execute the except block.
We would import a given module inside the try block. If the given module does not work in terms of version compatibility or python fails to import it, it will go to the except block.
If the thrown exception is similar to the exception in the except statement, then python would execute the lines of code written in except block.
try:
import module1
except ImportError:
import module2
Here first, we will try to import module1. If that does not work, then try will be thrown an ImportError, and except block will catch that error. And by doing so, it will import module2.
We can also use exception handling to check if a given module exists or not. Python will import a module only if it exists in the program and prevent the unnecessary error from being raised in the program. We can either print a message or pass.
try:
import module1
except ImportError:
pass
FAQ’s on python conditional import
Q. What is selective import in python?
A. In python, when we import using the import keyword, we are importing the entire package. But, instead, if we want to import only a single functionality, we use selective import. If we execute :
import sklearn
The above would import the entire sklearn module. But, if we want only to import the class linear_model, we will use selective import.
from sklearn import linear_model
Q. What is python dynamic import?
A. Python allows us to import modules dynamically. By doing so, we will be able to import modules without knowing beforehand which module exactly. There are two main functions for the importing modules dynamically –
__import__() function and importlib.import_module() function.
Q. What is optional import?
A. Optional Import is a library in python which is used for handling ImportError. If we use optional import, it will prevent ImportError from being raised.
Have any thought to share? We would love to hear in the comments.
Till then, Happy Learning!