
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Can we iteratively import python modules inside a for loop?
Yes you can iteratively import python modules inside a for loop. You need to have a list of modules you want to import as strings. You can use the inbuilt importlib.import_module(module_name) to import the modules. For example,
>>> import importlib >>> modnames = ["os", "sys", "math"] >>> for lib in modnames: ... globals()[lib] = importlib.import_module(lib)
The globals() call returns a dict. We can set the lib key for each library as the object returned to us on import of a module.
- Related Articles
- How we can import Python modules in Jython?
- How we can import Python modules without installing?
- How can I import modules for a Python Azure Function?
- Python import modules from Zip archives (zipimport)
- How we can bundle multiple python modules?
- How to handle exception inside a Python for loop?
- Can we keep Python modules in compiled format?
- Can we change Python for loop range (higher limit) at runtime?
- How to use multiple modules with Python import Statement?
- How to install and import Python modules at runtime?
- How to create a lambda inside a Python loop?
- How we can copy Python modules from one system to another?
- How to import Python modules by default at the time of program starts?
- How we can come out of an infinite loop in Python?
- Can we define a package after the import statement in Java?

Advertisements