Packages in Python

Last updated Aug 13, 2021

Packages in Python

The package is the hierarchical file directory structure used in the python programming language used to define a single python environment of application that contains several modules and sub-packages.

Now consider an environment we have created a file name whatsapp.py in my phone directory

 

def whatsapp():
print(“Whatsapp is in my phone”)

 

Similarly, we have made two other distinct functions but in the same directory

 

Facebook: myphone/facebook having function facebook()
Instagram: myphone/instagram having function instagram()

 

Now create another file name __init__() in the same directory

myphone/__init__()

 

to make all the functions available when the imported phone, we have to use the import statement in __init__() as done below:

 

from whatsapp import whatsapp
fom instagram add instagram
from facebook add facebook

 

now add these lines in __init__.py

import myphone
myphone.whatsapp
myphone.facebook
myphone.instagram

 

Whatsapp is in my phone

Facebook is in my phone

Instagram is in my phone

 

 

Difference between a Module and a Package

A file in Python is known as a Module whereas a collection of modules is known as a Package. A module consists of a single python file but a package is a collection of modules so this is the reason why a package is termed as a directory of modules.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

69 Views