

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 I can dynamically import Python module?
To dynamically import Python modules, you can use the importlib package's import_module(moduleName) function. You need to have moduleName as a string. For example,
>>> from importlib import import_module >>> moduleName = "os" >>> globals()[moduleName] = import_module(moduleName)
If you want to dynamically import a list of modules, you can even call this from a for loop. 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 Questions & Answers
- Import module in Python
- Import a module in Python
- How can import python module in IDE environment available in tutorialspoint?
- How I can install unidecode python module on Linux?
- How can I import modules for a Python Azure Function?
- How I can check a Python module version at runtime?
- How to import a single function from a Python module?
- How to import a Python module given the full path?
- How can I merge properties of two JavaScript objects dynamically?
- How do I unload (reload) a Python module?
- How we can instantiate different python classes dynamically?
- How we can import Python modules in Jython?
- How we can import Python modules without installing?
- How can I dynamically set the position of view in android?
- How to import and export a module/library in JavaScript?
Advertisements