Controlling what gets imported

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 ...

Get Modular Programming with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.