Leela Soft Python3 Madhu
Packages
✓ It is an encapsulation mechanism to group related modules into a single unit.
✓ The package is nothing but folder or directory which represents collection of Python
modules.
✓ A package can contains sub packages also.
Any folder or directory contains __init__.py file, is considered as a Python package. This
file can be empty.
The main advantages of package statement are
✓ We can resolve naming conflicts
✓ We can identify our components uniquely
✓ It improves modularity of the application
Example 1:
E:\8am>
|-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()
www.leelasoft.com Cell: 78-42-66-47-66 1
Leela Soft Python3 Madhu
Example 2:
E:\8am>
|--test.py
|--com
|--module1.py
|--__init__.py
|--leelasoft
|--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.leelasoft")
test.py:
from com.module1 import f1
from com.leelasoft.module2 import f2
f1()
f2()
www.leelasoft.com Cell: 78-42-66-47-66 2