Stats

1132 Contributors: 43
2017-08-02
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Importing modules

Download python eBook

Syntax

  • 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]

Remarks

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