Packages In Python



Example: -

- First of all, we need a directory.
- The name of this directory will be the name of the package, which we want to create. 
- We will call our package "pack1".
- This directory needs to contain a file with the name "__init__.py". 
- This file can be empty, or it can contain valid Python code. 
-  This code will be executed when a package will be imported, so it can be used to initialize a package, e.g. to make sure that some other modules are imported or some values set.
- Now we can put into this directory all the Python files which will be the submodules of our module.



- We create two simple files a.py and b.py just for the sake of filling the package with modules.

 The content of a.py:
def a1():  
            print("Hello, function 'a1' from module 'a' calling")
 The content of b.py:
def b1():  
            print("Hello, function 'b1' from module 'b' calling")

Note: __init__.py is simply a file that is used to consider the directories on the disk as the package of the Python. It is basically used to initialize the python packages.

Some Basic Information About This:-

 - A Package is simply a collection of similar modules, sub-packages etc. 
-  A package is basically a directory with Python files and a file with the name __init__.py.  This means that every directory inside of the Python path, which contains a file named __init__.py, will be treated as a package by Python.
- It's possible to put several modules into a Package.
- Packages are a way of structuring Python’s module namespace by using "dotted module names".  A.B stands for a submodule named B in a package named A. 
- Two different packages like P1 and P2 can both have modules with the same name, let's say A, for example. 
- The submodule A of the package P1 and the submodule A of the package P2 can be totally different. -  A package is imported like a "normal" module.






Demo Programm...

- Write a Python program that creates a package called “Lunch” which consists of fruit.py and vegetable.py. Fruit.py contains 3 function called apple(), banana(), orange(). Vegetable.py contains 2 function called potato() and tomato(). Create one main file outside the lunch package and import Lunch to access fruit.py and vegetable.py file using __init __.py file. Call all the functions in main file using the concept of Package.

2.py Prog.name

from Lunch import Fruit,Vegetable
Fruit.apple()
Fruit.banana()
Fruit.orange()
Vegetable.potato()
Vegetable.tomato()

_init_ Blank file

Create Fruit.py File And write This

def apple():
 print 'Apple'
def banana():
 print 'Banana'
def orange():
 print 'Orange'





Write IN vegetable.py file
def potato():
 print 'Potato'
def tomato():
 print 'Tomato'


If you Need More Demo then Comments I will upload PDF FILE So You Can easily Understands

Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)

Post a Comment

5 Comments