Python Packages

Python Packages 

  • It is an encapsulation mechanism to group related modules into a single unit. package is nothing but folder or directory which represents collection of Python modules.
  • Any folder or directory contains __init__.py file,is considered as a Python package.This file can be empty.
  • A package can contains sub packages also. 
  • A collection of various module with one special file that is __init__.py.

The main advantages of package statement are:

  • We can resolve naming conflicts
  • We can identify our components uniquely 
  • It improves modularity of the application 
Eg 1:

D:\Python_classes> 
           |-test.py        
           |-pack1               
                |-module1.py               
                |-__init__.py

__init__.py: 
empty file  

module1.py: 
def f1():  
         print("Hello this is from module1 present in pack1")

test.py (version-1): 
import pack1.module1 
pack1.module1.f1() 

test.py (version-2):

from pack1.module1 import f1 
f1() 

Eg 2:

D:\Python_classes>        
        |-test.py        
        |-com               
             |-module1.py               
             |-__init__.py        
                   |-programmerfect                       
                           |-module2.py                        
                           |-__init__.py
__init__.py:
 empty file

module1.py:

def f1():  print("Hello this is from module1 present in com")

module2.py:

def f2():  print("Hello this is from module2 present in com.durgasoft")  

Note: Summary diagram of library,packages,modules which contains functions,classes and variables.

 

 

Comments

  1. Very informative article, which you have shared here about the logo design packages . After reading your article I got very much information and it is very useful for us. I am thankful to you for sharing this article here.logo design packages in London

    ReplyDelete

Post a Comment