When you import a module or package, or when you use a wildcard import such as from my_module import *
, the Python interpreter loads the contents of the given module or package into your global namespace. If you are importing from a module, all of the top-level functions, constants, classes, and other definitions will be imported. When importing from a package, all of the top-level functions, constants, and so on defined in the package's __init__.py
file will be imported.
By default, these imports load everything from the given module or package. The only exception is that a wildcard import will automatically skip any function, constant, class, or other definition starting with an underscore—this has the effect of excluding private definitions from the wildcard import.
While this default behavior generally works well, there are times when you may want more control over what gets imported. To do this, you can use a special variable named __all__
.
To see how the...