But what if the name of the required module is known to us only at runtime? How can we import this module? You can use the built-in
__import__() . It also helps to import modules at runtime.
Syntax: __import __ (name, globals, locals, fromlist, level)
name : Name of the module to be imported
globals and locals : Interpret names
formlist : Objects or submodules to be imported (as a list)
level : Specifies whether to use absolute or relative imports. Default is -1 (absolute and relative).
Example # 1 :
& lt; class `numpy.ndarray` & gt;
Example # 2 :
Both of the following statements have the same meaning and do the same job.
__import__() is not really required in day to day Python programming. Its direct use is rare. But sometimes, when you need to import modules at runtime, this feature is quite useful.
A recipe for having fun and getting things done with the Raspberry Pi ...
As the title promises, this book will introduce you to one of the world’s most popular programming languages: Python. It’s aimed at beginning programmers as well as more experienced programmers wh...
Become a master in Python by learning coding best practices and advanced programming concepts in Python 3.7. This book is written for Python developers who wish to go further in mastering Python. A...
This book contains chapters authored by several leading experts in the field of cloud computing. The book is presented in a coordinated and integrated manner starting with the fundamentals and followe...