- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to do multiple imports in Python?
Importing modules is an essential part of programming in Python, and it's something you'll do a lot as you develop your skills. In Python, you can import multiple modules using a few different methods. Here are some examples of how to do it:
Import each module on a separate line
The simplest way to import multiple modules in Python is to import each one on a separate line. For example, suppose you want to import the math, random, and time modules. You could do it like this:
This code tells Python to import the math, random, and time modules and make them available for use in your code.
import math import random import time
Use comma-separated import statements
Another way to import multiple modules is to use a comma-separated list of import statements.
This code does the same thing as the previous example, but it uses a single import statement with a comma-separated list of module names.
import math, random, time
Use the from...import statement
The third way to import multiple modules is to use the from...import statement. This allows you to import specific functions or objects from a module and make them available for use in your code.
This code tells Python to import the pi constant from the math module, the randint function from the random module, and the sleep function from the time module.
from math import pi from random import randint from time import sleep
It must be noted that in each of these examples, we're importing modules from the standard library that comes with Python. If you want to import modules from third-party libraries or modules you've created yourself, you'll need to make sure they're installed and accessible in your Python environment before you can import them.
Import all modules using the * operator
Another way to import multiple modules is to use the * operator to import all the functions and objects from a module. For example:
This code tells Python to import all the functions and objects from the math, random, and time modules and make them available for use in your code. However, this method is not recommended because it can lead to naming conflicts and makes it harder to read your code.
from math import * from random import * from time import *
Use aliases for module names
If you have two modules with the same name or you want to use a shorter name for a module, you can use aliases to import them.
This code tells Python to import the math, random, and time modules and give them aliases of m, r, and t, respectively. You can then use the aliases instead of the full module names in your code.
import math as m import random as r import time as t
Import modules in a nested way
You can also import modules in a nested way, which allows you to organize your imports and reduce the chance of naming conflicts.
This code tells Python to import the NumPy module and give it an alias of np, and then import the Pandas module and give it an alias of pd. This is a common way to import modules in scientific computing and data analysis.
import numpy as np import pandas as pd
Importing a list of modules
If you have a list of module names as strings and you want to import them dynamically at runtime, you can use the built-in importlib module in Python. Here’s how to do it:
In this example, we first import the importlib module, which provides functions for working with dynamic imports. We then define a list of module names as strings, and a list to store the imported modules.
Next, we loop through the list of module names and use the import_module() function from the importlib module to dynamically import each module. The imported module is then appended to the imported_modules list.
Finally, we loop through the imported_modules list and print the pi constant from each module.
import importlib # List of module names as strings module_names = ['math', 'random', 'time'] # List to store imported modules imported_modules =  # Loop through the module names and import them for name in module_names: imported_module = importlib.import_module(name) imported_modules.append(imported_module) # Now you can use the imported modules in your code for module in imported_modules: print(module.pi)
It should be noted that this approach is useful if you don't know ahead of time which modules you need to import, or if you want to load modules dynamically based on user input or other conditions. However, it's generally better to use the standard import statement to import modules directly if you know which modules you need to use in your code.
Kickstart Your Career
Get certified by completing the courseGet Started