Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - PACKAGE


No matter if you are a Windows or Mac user, you always use folders to keep your files inside it. So that all the related files are grouped together.


Same applies for Python as well. To group all the related Modules together, we use 'Package'.


A 'Package' is a collection of all related 'Modules'.


Say for example, you have four Modules for Addition, Subtraction, Multiplication and Division.


Now, all the four Modules does some kind of similar task. So, it would be a great idea to group them together, in a so called Folder.


Well! In Python it is not actually a Folder. It is called a 'Package'.


Let us see the below example.


Example :


So, we will be creating four modules, 'add.py', 'sub.py', 'mul.py' and 'div.py'. And define the Functions in them.

add.py


def addition(x, y):
    z = x + y
    return z

sub.py


def subtraction(x, y):
    z = x - y
    return z

mul.py


def multiplication(x, y):
    z = x * y
    return z

div.py


def division(x, y):
    z = x + y
    return z


Then we need to create a directory/folder. And place the all the four modules, 'add.py', 'sub.py', 'mul.py' and 'div.py' in that folder.


Let us name the folder as 'calculation'.


Now, we need to ensure that this 'calculation' directory/folder would behave as a Python 'Package'.


And all we need to do is, create a file called '__init__.py', and add it inside the 'calculation' directory/folder.


And Python treats the 'calculation' directory/folder as a 'Package'.


java_Collections

Just remember, '__init__.py' can be empty but for a directory to behave like a 'Package', '__init__.py' must be present in that directory.


Now, let us say, we want to access the 'addition(...)' Function that is inside the 'add.py' Module.


add.py


def addition(x, y):
    z = x + y
    return z


Also the 'add.py' Module is inside the 'calculation', 'Package'.


And just like importing the Module, we can use the 'import' keyword to import the 'calculation','Package'.


Let us see in the below example.


Example :


import calculation.add
x = calculation.add.addition(9,8)
print(x)


Output :



  17

So, in the first line, we have used the 'import' keyword followed by the 'Package' name (i.e. 'calculation'), followed by the 'Module' name(i.e. 'add').


import calculation.add

java_Collections

In the next line, we have called the 'addition(...)' Function, using the 'Package' name (i.e. 'calculation'), followed by the 'Module' name(i.e. 'add').


x = calculation.add.addition(9,8)

And that's all.


Now, let's say, we don't want to type the 'Package' name (i.e. 'calculation'), followed by the 'Module' name(i.e. 'add'), every time we call a Function.


i.e. 'calculation.add.addition(9,8)'


And Python has a solution for that as well using the 'from' keyword.


The 'from' keyword with import


Example :


from calculation import add
x = add.addition(9,8)
print(x)


Output :



  17

So with the 'from' keyword along with 'import'.


from calculation import add

java_Collections

And we don't have to mention the entire path and just 'add.addition(9,8)' is enough to call the Function.


x = add.addition(9,8)

Now, let us say, we have a requirement to call the 'addition(...)' and 'multiplication(...)' Function.


So, as we know, 'addition(...)' is in 'add.py' Module and 'multiplication(...)' is in 'mul.py' Package.


We need to write the 'import' statement that imports both the modules.


Example :


from calculation import add,mul
x = add.addition(9,8)
print(x)
y = mul.multiplication(5,4)
print(y)    


Output :



  17
  20

And all we have done is, in the 'import' statement, imported both the Modules 'add.py' and 'mul.py' using comma ','.


from calculation import add,mul

java_Collections

And we were able to call both the Functions and print them.


x = add.addition(9,8)
print(x)
y = mul.multiplication(5,4)
print(y)