Python Packages PDF
Python Packages PDF
Complete
Python
In
Simple Way
1 https://www.youtube.com/durgasoftware
PACKAGES
STUDY MATERIAL
2 https://www.youtube.com/durgasoftware
֍ 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.
__init__.py
File 1
File 1 File 1
__init__.py __init__.py
Loan
__init__.py
File 1
File 1 File 1
__init__.py __init__.py
Loan
The main advantages of package statement are
1) We can resolve naming conflicts
2) We can identify our components uniquely
3 https://www.youtube.com/durgasoftware
3) 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
|-durgasoft
|-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")
4 https://www.youtube.com/durgasoftware
test.py
Output
D:\Python_classes>py test.py
Hello this is from module1 present in com
Hello this is from module2 present in com.durgasoft
Note: Summary diagram of library, packages, modules which contains functions, classes
and variables.
Library
5 https://www.youtube.com/durgasoftware