Official Documentation


For example, the statement import spam results in bytecode resembling the following code:

spam = __import__('spam', globals(), locals(), [], 0)

The statement import spam.ham results in this call:

spam = __import__('spam.ham', globals(), locals(), [], 0)

Note how __import__() returns the toplevel module here because this is the object that is bound to a name by the import statement.

On the other hand, the statement from spam.ham import eggs, sausage as saus results in:

_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], 0)
eggs = _temp.eggs
saus = _temp.sausage

I like to use it in combo with globals(), locals() and getattr(), doing so i can generalize the import metod using strings:

import Package.function
Package.function = Package.function.function
#I usually declare only one function with the same 
#name of the file 
#ITS EQUAL OF DOING:
locals()['Package.function'] = __import__(
	'Package.function',
	globals(),
	locals(),
	['function'],
	0,
)
locals()['Package.function'] = getattr(
	locals()['Package.function'],
	'function'
)