ModulesΒΆ

  • Modules are Python’s way of organizing libraries of variables, functions and classes.
  • Every source (text) file is a separate module.
  • Directory trees of python scripts define module hierarchies.
  • Modules are imported to make their contents available:
>>> import Bag    # Assume Bag class defined in file Bag.py.
>>> b = Bag.Bag(1, 2, 3)
  • Use from/as to give objects alias names:
>>> from Bag import Bag as box
>>> b = box(1,2,3)
>>> type(b)
<class 'Bag.Bag'>
  • Set the PYTHONPATH environment variable to search for modules in other directories.