Modules & Packages

A module is a python source file containing function definitions.

moduleSo if I wanted to group a bunch of similar functions together and be able to use it anywhere else I'd create a module. For example, say I want to create a module that has two functions - add2() and add3(). Then I would need to create a new python file and call it add.py and place both functions into it. From the interpretor I can import the module like so

>>>import add

and call the functions

>>>add.add2(2,3)

5

>>add.add3(1,2,3)

6

Now a package in python is a group of modules and an initialization file in a folder.

module.py + __init__.py = package

Let's create an arithmetic package, for this we need to create a folder called arithmetic, inside it place the modules you want and __init__.py file. The __init__.py file is for the initialization of the package which can be empty for now.

package1

If I want to use the arithmetic package, I can do so by importing it

>>>from arithmetic.addition import add2

>>>add2(3,2)

5

You can read up more about modules and packages from over here https://docs.python.org/2/tutorial/modules.html