Modules and packages python

November 11, 2017
  

Most of it has content from stackoverflow question.

  • Modules :
    • These are simple python files with the .py extension, which implement a set of functions/functionality.
    • Modules are imported from other modules using the import command like import file
    • Above will import variables/funtions/classes from file.py.
  • Packages :
    • Package is a directory of Python modules containing an additional init.py file.
    • For python package or module are of same type
          >>> import some_module
          >>> type(some_module)
          <type 'module'>
          >>> import some_package
          >>> type(some_package)
          <type 'module'>
      
    • When we import module we get all it’s function, classes and variable defined in it.
    • When we import package only variables/functions/classes in the init.py file of that package are directly visible, not sub-packages or modules.
    • The directory to be identified as package by python, should have init.py file in that directory
    • Packages can have sub-packages, those directories should have init.py file in the sub-directory.

Please give feedback at email@murarisumit.in