Module Built-in Functions

The importation of modules has some functional support from the system. We will look at those now.


The __import__() function is new as of Python 1.5, and it is the function that actually does the importing, meaning that the import statement invokes the __import__() function to do its work. The purpose of making this a function is to allow for overriding it if one is inclined to develop his or her own importation algorithm.
The syntax of __import__() is:

__import__(module_name[, globals[, locals[, from list]]]

The module_name variable is the name of the module to import, globals is the dictionary of current names in the global symbol table, locals is the dictionary of current names in the local symbol table, and fromlist is a list of symbols to import the way they would be imported using the from-import statement.
The globals, locals, and fromlist arguments are optional, and if not provided, default to globals(), locals(), and [], respectively.
Calling ‘import sys’ can be accomplished with

sys = __import__('sys')

You may also like...