Importing modules

Download python eBook


  • import module_name
  • import module_name.submodule_name
  • from module_name import *
  • from module_name import submodule_name [, class_name, function_name, ...etc]
  • from module_name import some_name as new_name
  • from module_name.submodule_name import class_name [, function_name, ...etc]


Importing a module will make Python evaluate all top-level code in this module so it learns all the functions, classes, and variables that the module contains. When you want a module of yours to be imported somewhere else, be careful with your top-level code, and encapsulate it into if __name__ == '__main__': if you don't want it to be executed when the module gets imported.

Related Examples


1132 Contributors: 43
Wednesday, August 2, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook